2015-09-10 56 views
5

我正在用javascript构建一个国际象棋游戏,并且对使用继承的正确方法有点不确定。在代码中的一个部分,我有不同的片类型的扩展它,例如与骑士一块对象(因为它是最短的),它看起来像这样(没有评论):在JavaScript中,我应该添加函数到对象还是对象原型

/************* 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; 
}; 

,它工作正常,如你所期望的物体看起来根据Chrome的输出的console.log如下:在代码中的不同的文件

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

现在,我有一个方形物体的定义,哪个厕所KS这样的:

广场

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); 
    } 
}; 

这也工作得很好,而且,你可能会想到,对象的控制台日志如下:

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

这也工作正常。所以我的问题是,在哪种情况下哪种方法更好?两个对象除了具有作为属性的功能以外,还有另一个作为原型的属性之间有什么区别?

回答

3

写入的实际优选的或推荐的方法是作为对象:

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); 
}; 

参考: http://javascript.crockford.com/prototypal.html

随着中说,许多顶级项目中使用的其它,以及包括原型,图案(多个) 。

0

函数添加到对象对于每个对象实例都是唯一的;向原型添加的功能 将与该对象的每个实例相同; that`s的尊重,你需要

1

属性添加到prototype运行一次,并且不要选择运行后,同时creating objectsproperties加入this当你创建一个new object运行所有的时间。

当您尝试访问propertiespropertyobject先来看看添加到thisobject。如果在此object未找到,然后在prototype,然后prototype chain,直到父母达到或在其中找到属性。

Javascript支持原型继承。所以最好在原型中添加属性。

this参考文献也存储在本地变量

function Piece(square, color) { 
    var self = this; 
    self.square = square; 
    self.color = color; 
} 
内部
相关问题