2015-09-10 19 views
5

nesne eklemek gerekir Şimdi javascript bir satranç oyunu yapıyorum ve miras ile çalışmak için doğru yolu biraz emin değilim. Bir kod kısmında farklı parça türleri bir şövalye ile örneğin, o uzanan bir parça nesne var onu (yorumsuz) şöyle (Bu en kısa var gibi):javascript, ben nesnelere veya nesne prototip

adet

/************* piece ********************/ 
function Piece(square, color) { 
    this.square = square; 
    this.color = color; 
} 

Piece.prototype.get_path = function(to) { 
    return null; 
}; 

Piece.prototype.get_capture_path = function(to) { 
    return this.get_path(to); 
}; 

Piece.prototype.isAt= function(square) { 
    return (this.square.equals(square)); 
};  

/************* KNIGHT *****************/ 
Knight.prototype = Object.create(Piece.prototype); 
Knight.prototype.constructor = Knight; 
function Knight(square, color) { 
    Piece.call(this, square, color); 

    this.type = KNIGHT; 
} 

Knight.prototype.get_path = function(to) { 
    var h_movement = Math.abs(this.square.file - to.file); 
    var v_movement = Math.abs(this.square.rank - to.rank); 

    if ((h_movement === 2 && v_movement === 1) || (h_movement === 1 && v_movement === 2)) { 
     return [to]; 
    } 
    return null; 
}; 

Ve Chrome'un console.log çıkışına göre izleyin olarak nesne görünüyor beklendiği gibi, çalışıyor: bir kare nesnenin tanımı var,

Knight {square: Square, color: "w", type: "N"} 
color: "w" 
square: Square 
type: "N" 
__proto__: Knight 
    constructor: Knight(square, color) 
    get_path: (to) 
    __proto__: Piece 

Şimdi farklı bir kod dosyasında, hangi loo Böyle ks:

Kare

function Square(file, rank) { 
    this.file = file; 
    this.rank = rank; 

    this.equals = function(other) { 
     return (this.file === other.file && this.rank === other.rank); 
    }; 

    this.toString = function() { 
     return String(file) + ' ' + String(rank); 
    }; 

    this.getSquareAtOffset = function(file, rank) { 
     file = Number(file)? file : 0; 
     rank = Number(rank)? rank : 0; 
     return new Square(this.file + file, this.rank + rank); 
    } 
}; 

beklediğiniz muhtemelen gibi aynı zamanda, gayet iyi çalışıyor ve bu nesnenin konsol günlüğü şu şekilde olmaktadır: Ayrıca

Square {file: 1, rank: 1} 
equals: (other) 
file: 1 
getSquareAtOffset: (file, rank) 
rank: 1 
toString:() 
__proto__: Square 

Bu iyi çalışıyor. Yani sorum şu, hangi yaklaşımın hangi durumda daha iyi olduğu? Bir özellik olarak işlev gören ve diğeri de prototip özelliğine sahip olan iki nesne arasındaki fark nedir?

cevap

3

yazılı gerçek tercih edilen veya önerilen yolu, bir nesne gibidir:

var Square = {}; 
Square.file = file; 
Square.rank = rank; 
Square.equals = function(other) { 
    return (this.file === other.file && this.rank === other.rank); 
}; 
Square.toString = function() { 
    return String(file) + ' ' + String(rank); 
}; 
Square.getSquareAtOffset = function(file, rank) { 
    file = Number(file)? file : 0; 
    rank = Number(rank)? rank : 0; 
    return new Square(this.file + file, this.rank + rank); 
}; 

Referans: bahsedilen çok sayıda üst proje ile http://javascript.crockford.com/prototypal.html

prototip, desen dahil olmak üzere diğer kullanımı, ve (lar) .

0

nesneye eklenen işlevler, nesnenin her bir örneğine özgü olacaktır; Prototipine işlevlerinin eklenmesi, nesnenin her bir örneği ile aynı olacaktır; dikkatealarak hepsi bu kadar,

1

Özellikleri kez prototype çalışır eklendi ve dont sizin ihtiyaç tarafından tercih Bir new object oluştururken bütün kez çalışır sonra ise creating objects ama propertiesthis eklenen çalışır.

Eğer properties içinde propertyobject ilk bakış erişmeye çalıştığınızda

this object eklendi. Bu object ve ardından prototype ve ardından prototype chain numaralı bağlantıda bulunamadıysa, üst öğeye erişinceye veya aralarında bulunan özellik bulunana kadar.

Javascript prototip kalıtımını destekler. Bu yüzden özellikleri prototip olarak eklemek iyidir.

da yerel bir değişken

function Piece(square, color) { 
    var self = this; 
    self.square = square; 
    self.color = color; 
} 
içinde this başvuru depolamak
İlgili konular