首页 > 代码库 > 原型实例

原型实例

function Fruits() {}
Fruits.prototype = {
color: ‘red‘,
say: function(num1, num2) {
return ‘i am ‘ + this.color + num1 + ‘ and ‘ + num2;
}
};
var apple = new Fruits();
alert(apple.say(1,2));
banana = {
color:‘yellow‘
};

alert(Fruits.prototype.say.call(banana, [1, 2]));
// alert(Fruits.prototype.say.apply(banana, [1, 2]));

会有出错,call的【1,2】被解析为字符串1,2放入num1,而num2为undefined;

原型实例