I trying to understand this piece of code
Function.prototype.method = function (name, fn) {
this.prototype[name] = fn;
return this;
};
what does "this" refering to in the function body ?
Is it refering to Function.prototype
?
Is it trying to add a member to
Function.prototype.prototype
, namely Function.prototype.prototype[name]
?
Function in Function.prototype
are called on Function
instances.
Therefore, this
refers to the function you called it on.
this.prototype
would refer to the prototype of the function you called it on.
For example:
function MyClass() { }
MyClass.method("myMethod", function() { });
var c = new MyClass();
c.myMethod(); //MyClass.prototype.myMethod