function A(name){
this.name=name;
}
If A===A.prototype.constructor
then invoking both of them does not set the name variable on window object
Only A("abhishek")
sets the variable on window and A.prototype.constructor("randommm")
does not!
Can someone please explain what's the difference, I am thinking that
A.prototype.constructor("randommm")
is called from a different context?
Yes, they run in different contexts.
You're creating a hoisted function in the local scope, which means its default context, unless you bind it to something else, will be window
(or at any rate the global namespace) when you invoke it.
Calling it via prototype.constructor
, though, calls it as a method of the prototype
object, so it's logical that the prototype is the context.
function A() { alert(this); }
A(); //context = window
A.prototype.constructor(); //context = prototype