【发布时间】:2015-03-12 12:06:39
【问题描述】:
我有这个代码:
function Shape() {}
Shape.prototype.init = function(coords, color) {
for(var i=0;i<coords.length;i++)
var block= new Block (coords[i],color);
};
Shape.prototype.draw = function() {
};
// ============= I_Shape ================================
function I_Shape(center) {
var coords = [new Point(center.x - 2, center.y),
new Point(center.x - 1, center.y),
new Point(center.x , center.y),
new Point(center.x + 1, center.y)];
Shape.prototype.init.call(this, coords, "blue");
}
I_Shape.prototype = new Shape();
I_Shape.prototype.constructor = I_Shape;
我想知道为什么在 I_Shape 函数的最后一行有一个“init.call(...)”。是否有必要(在父函数中不使用上下文),还是可以换成下面这行?
Shape.prototype.init(coords,"blue");
显然在第一个中您传递了上下文,但除此之外,这两者之间有什么区别?
【问题讨论】:
标签: javascript prototype