var obj = {num:2};
var add = function(a){
return this.num + a;
};
add.call(obj, 1); //function.call(obj, arg)
2)调用链构造为一个对象。
var Product = function (name, price) {
this.name = name;
this.price = price;
}
var Food = function(name, price) {
Product.call(this, name, price); // <-- 1. Product is obj constructor not fun
// 2. what is 'this' here?
this.category = 'food';
}
var cheese = new Food('feta', 5);
console.dir(cheese);
我目前研究的JavaScript接力,我发现了一个例子约Function.prototype.call()
链构造函数,但我不明白它是如何工作的。
1)是否需要调用函数调用obj?但产品是构造。
var food = { category : 'food' };
var Product = function (name, price) {
this.name = name;
this.price = price;
}
var cheese = Product.call(food);
console.dir(cheese);//this will become undefine
2)'this'是什么?食物对象? Product.call(new Food, name, price);
?
*最终结果var cheese
将是一个对象,Product.call(new Food obj, name, price)
产品仍然是一个函数,所以结果成为一个对象?
'this'将指向'Food' – Rajesh
其真正混淆,因为食品是一个构造函数,我们创建了一个新的食品对象,里面含有叫 –
我越来越混淆这些,我们有函数,方法和Constructor,什么是类的功能? –