What does this.constructor.prototype.constructor.apply mean in js

I read this article and I cannot figure out what the line below does. Even if I delete this line, I see no difference.

this.constructor.prototype.constructor.apply(this,Array.prototype.slice.call(arguments));

Can someone explain to me why this.constructor.prototype.constructor necessary, will this.constructor return the same value?

+4
source share
1 answer

Basically, what he is trying to do is call the constructor function for the object, which is the prototype of the current object.

Destruction:

  • this - current object
  • this.constructor is the constructor property on the current object, which is almost certainly (but not necessarily!) inherited from its prototype. And therefore, it is possible (but not necessary) the function that created the object.
  • this.constructor.prototype - The object assigned to this prototype property. (The object that will be assigned as the base prototype of the objects created if you call this function through new .)
  • this.constructor.prototype.constructor - The function that created this object.

... and then we'll call apply on it to set this during the current call to this and use Array.prototype.slice to make a copy of the current arguments as a true array. (This last part is unnecessary, apply accepts everything that looks like an array, does not require true arrays. Thus, the code can just use arguments .)

+4
source

Source: https://habr.com/ru/post/1486397/


All Articles