user33090
user33090

Reputation: 261

What is the purpose of `this.prototype.constructor = this;`?

In the ASP.NET ajax library, there is a line that makes me confused.

Type.prototype.registerClass = function Type$registerClass(typeName, baseType, interfaceTypes) {

//..

this.prototype.constructor = this;

//..
}

I know that (this.prototype.constructor === this) == true, so what is significance of this line? I remove the line, and test the library with some code. It seems it is okay. What is the purpose of this line?

Upvotes: 4

Views: 390

Answers (2)

zatatatata
zatatatata

Reputation: 4821

My guess would be that at some point before this.prototype.constructor = this;, some object was assigned to the prototype property, which overwrote prototype.constructor. This trick is often used when inheriting object prototypes easily, but still being able to call instanceof to see whether an object instance is of a certain type.

Hard to tell anything more specific than that in this case and a seriously old question, however it might be useful to somebody.

Upvotes: 1

Justin Love
Justin Love

Reputation: 4457

I'm not familiar with the asp.net libs, but:

A common pattern in Javascript, especially when trying to simulate class based systems, is to reassign the prototype object to an instance of another object, rather than just adding properties to the prototype object JS gives you. One issue with this is that it gives you the wrong constructor - unless perhaps one resets with a 'correct' value.

Upvotes: 1

Related Questions