Understanding JavaScript Prototype

Function is its own constructor (i.e. "parent").

Function.constructor;  // function Function() { [native code] }
Normally you can't do what you're doing. For example, this won't work:

f = function () {};
f.prototype.a = 5;
f.a;  // undefined
This kind of thing only works if you use a function as a constructor, like so:

f = function () {};
f.prototype.a = 5;
g = new f();
g.a;  // 5
But Function is weird, it is the constructor for all functions and is also a function itself, so it templates its properties off its own prototype. Hence you can call Function.method() in your code.

For more info about prototypal inheritance visit http://javascript.info/tutorial/inheritance
Source: Stackoverflow.

Comments

Popular posts from this blog

How SQL Indexes Work internally ?

UML,HLD and LLD.

Mutable and Immutable Objects