开发者

Difference between call() function and calling implicit constructor of inherited class

What is the difference between calling a function call(), and calling implicit constructor of a inherited clas开发者_开发百科s?

Example1:

function DepthRectangle(w, h, d){
    Rectangle.call(this, w, h);
    this.depth = d;
}

Example2:

function DepthRectangle(w, h, d){
    this = new Rectangle(w, h);
    this.depth = d;
}

Constructor of Rectangle:

function Rectangle(w, h){
    this.width = w;
    this.height = h;
}


Well, your second example doesn't work, because the this value is not assignable, it isn't a valid left-hand side expression for assignments.

But, suppose that you have:

function DepthRectangle(w, h, d){
    var obj = new Rectangle(w, h);
    obj.depth = d;
    return obj;
}

In this case, the difference would be that the object returned by this function will inherit directly from Rectangle.prototype, and not from DepthRectangle.

For example, with the above function:

//...
DepthRectangle.prototype.inherited = "foo";
Rectangle.prototype.inherited = "bar";

var obj = new DepthRectangle(1,2,3); // note: new is not really needed
obj.inherited; // "bar", from Rectangle.prototype
obj instanceof DepthRectangle; // false

In your first example, using the call method will just execute the Rectangle function, but with the this value pointing to the object that was created on DepthRectangle, meaning that this object will in fact still inherit from DepthRectangle.prototype.


Well, for one, assigning this = new Rectangle(w, h) will probably throw

Uncaught ReferenceError: Invalid left-hand side in assignment

.call() basically lets you do the same thing you're wanting to do. It works by allowing you to set the value of this within the function you're calling. If you're calling another functions constructor, it will apply to your constructor to your current this object.

Mind you, the prototype of Rectangle wouldn't be copied in this fashion, and you need to account for that if you want it to be.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/call


What is the difference between calling a function call(), and calling implicit constructor of a inherited class?

There are no implicit constructors in JavaScript.

The code

Rectangle.call(this, w, h);

...will ensure that within Rectangle, this refers to the object that this refers to in the calling code.

In the code

new Rectangle(w, h);

...this within Rectangle will refer to a new object created by the new operator.

Re Example 2:

this = new Rectangle(w, h);

You can't assign to this. this is set by how a function is called, and cannot be changed during the function's processing.

Specifically, the

Rectangle.call(this, w, h);

idiom is used when creating hierarchies to ensure that the freshly-created object has been initialized with Rectangle things, presumably because the current constructor (DepthRectangle in your case) wants to re-use that functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜