Reputation: 1657
I am reading the book by Douglas Crockford, and he uses the construct of
Function.method('inherits', function(Parent){
this.prototype=new Parent();
return this;
});
If we leave alone the meaning of it, I simply can't get around the syntax. I try to run it in chrome, and get
Uncaught TypeError: undefined is not a function test3.html:18
(anonymous function)
as also happens with if I try (jsfiddle)
Function.method("test", function () { return "TEST"; });
There seems to be a post which says this line is working, but I can't make it work. Why can it be?
Upvotes: 0
Views: 59
Reputation: 159905
The reason that line is working in the post you refer to is because Function.prototype
has been extended with the method:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
If you run the above code and only then run the code you have, everything will work - or you can just change .method
to .prototype[name_here] =
and everything will work the same.
If you are going to extend prototypes in this day and age it is better to use Object.defineProperty
to ensure that the method is not enumerable.
Upvotes: 4