在javascript中,我应该向对象或对象原型添加函数
In javascript, should I add functions to object or object prototype
我现在正在 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
现在在不同的代码文件中,我有一个正方形对象的定义,它看起来像这样:
方形
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
这也很好用。所以我的问题是,哪种方法在哪种情况下更好?这两个对象之间有什么区别,一个对象的功能是原型的 属性 而另一个对象的功能是原型的 属性?
实际首选或推荐的写法是作为对象:
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
话虽如此,许多顶级项目使用其他模式,包括原型模式。
添加到对象的函数对于对象的每个实例都是唯一的;
添加到原型的函数将与对象的每个实例相同;
就是这样,根据需要选择
添加到 prototype
的属性运行一次,以后不再运行,而 creating objects
但添加到 this
的 properties
在您创建 [=15= 时一直运行].
当你试图访问一个property
时,首先查看object
里面的properties
添加到this
object
。如果在此 object
中找不到,然后在 prototype
中找到,然后在 prototype chain
中找到父级,或者在两者之间找到 属性。
Javascript 支持原型继承。所以在prototype中添加属性就好了
还将 this
引用存储在局部变量中
function Piece(square, color) {
var self = this;
self.square = square;
self.color = color;
}
我现在正在 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
现在在不同的代码文件中,我有一个正方形对象的定义,它看起来像这样:
方形
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
这也很好用。所以我的问题是,哪种方法在哪种情况下更好?这两个对象之间有什么区别,一个对象的功能是原型的 属性 而另一个对象的功能是原型的 属性?
实际首选或推荐的写法是作为对象:
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
话虽如此,许多顶级项目使用其他模式,包括原型模式。
添加到对象的函数对于对象的每个实例都是唯一的; 添加到原型的函数将与对象的每个实例相同; 就是这样,根据需要选择
添加到 prototype
的属性运行一次,以后不再运行,而 creating objects
但添加到 this
的 properties
在您创建 [=15= 时一直运行].
当你试图访问一个property
时,首先查看object
里面的properties
添加到this
object
。如果在此 object
中找不到,然后在 prototype
中找到,然后在 prototype chain
中找到父级,或者在两者之间找到 属性。
Javascript 支持原型继承。所以在prototype中添加属性就好了
还将 this
引用存储在局部变量中
function Piece(square, color) {
var self = this;
self.square = square;
self.color = color;
}