I read the book "Javascript: the good parts."
Now I am reading a chapter on additional types:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
UPDATE:
Why is the following code not working?
js> Function.prototype.method("test", function(){print("TEST")});
typein:2: TypeError: this.prototype is undefined
But the following code works without problems:
js> Function.method("test", function(){print("TEST")});
function Function() {[native code]}
Why does this code work?
js> var obj = {"status" : "ST"};
js> typeof obj;
"object"
js> obj.method = function(){print(this.status)};
(function () {print(this.status);})
js> obj.method();
ST
"obj" is an object.
But I can call the method "method" on it.
What is the difference between Function.prototype.method and obj.method?
source
share