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,什麼是類的功能? –