简体   繁体   English

对对象的链构造函数的Java语言调用

[英]Javascript call to chain constructors for an object

1) call 1)致电

var obj = {num:2};

var add = function(a){
    return this.num + a;
};

add.call(obj, 1); //function.call(obj, arg)

2) call to chain constructors for an object. 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);

I'm currently study javascript oop, I found an example about Function.prototype.call() chain constructors, but I don't understand how it works. 我目前正在研究javascript oop,我发现了一个有关Function.prototype.call()链构造Function.prototype.call()的示例,但我不知道它是如何工作的。

1) isn't call required function call obj? 1)不是需要调用的函数调用obj吗? but Product is construct. 但是产品是结构。

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) what is 'this'? 2)什么是“这”? Food object? 食物对象? Product.call(new Food, name, price); ?

*final result var cheese will be an object, Product.call(new Food obj, name, price) *最终结果var cheese将是对象Product.call(new Food obj, name, price)

Product still a function, why result become an object? 产品还是功能,为什么结果成为对象?

Both Product and Food are used as constructors, which means nothing more than that they are functions, and they can be called with new (but don't necessarily have to). ProductFood都用作构造函数,这意味着它们只是函数,可以用new调用它们(但不一定必须使用它们)。 The Food constructor makes use of Product to extend the object that is created with new Food(...) . Food构造函数利用Product扩展使用new Food(...)创建的对象。

It is vital to understand that when you call a function with new , a new, empty object is created that is available to that function as this . 至关重要的是要理解,当您使用new调用函数时,会创建一个新的空对象,该对象可用于this In the function you can then add properties to it. 然后,您可以在该函数中添加属性。 Also, if a function does not have a return statement in it, it still will return an object when it is called with new : it is the constructed object that is called this within the function. 另外,如果函数中没有return语句,则在使用new调用this函数时,它仍将返回一个对象:在该函数中被调用的是构造对象。

Some magic happens with: 一些魔术发生在:

Product.call(this, name, price); // <-- 1. Product is construct not fun

Normally you would call Product like this: 通常,您将这样称呼产品

new Product(name, price)

... but that creates and returns a new object. ...但这会创建并返回一个新对象。 However, here the purpose is to extend the object created with new Food(...) . 但是,这里的目的是扩展使用new Food(...)创建的对象。 That latter object is this . 后一个对象是this So instead of doing new Product(...) (which creates another new object), you pass the already existing object ( this ) to it. 因此,您无需执行new Product(...) (它会创建另一个新对象),而是将已经存在的对象( this )传递给它。 That you can do with .call() , which expects as the first argument the context -- the object that will be this during the Product function execution. 您可以使用.call() ,后者希望将上下文作为第一个参数,即在Product函数执行期间将成this对象的对象。

Note that in this case .call returns undefined , because the function is not called with new , and so the return value would be whatever the function returns explicitly. 请注意,在这种情况下, .call返回undefined ,因为该函数未使用new调用,因此返回值将是该函数显式返回的值。 As there is no return statement in Product , it will return undefined . 由于Product中没有return语句,因此它将返回undefined But that is of no interest here, as we only look to the side-effect the function call has on the first argument: this gets properties assigned to it. 但这在这里没有意义,因为我们仅关注函数调用对第一个参数的副作用: this将为其分配属性。

So both the Product and the Food function get to add properties to the same object. 因此, ProductFood函数都可以向同一对象添加属性。 When Food returns from the new Food() call, you'll have an object that is an instanceof Food . Foodnew Food()调用返回时,您将拥有一个对象,该对象是Foodinstanceof

Note that there are several ways to implement inheritance, and this way of doing it has some downsides, including: 请注意,有几种实现继承的方法,这种实现方法有一些缺点,包括:

  • the resulting object is not considered instanceof Product , and 结果对象不视为Product的 instanceof ,并且
  • properties which were defined on the Product.prototype will not be available to the object created with new Food() . 使用new Food()创建的对象将无法使用Product.prototype上定义的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM