Skip to content Skip to sidebar Skip to footer

No "function.method" In Javascript?

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 l

Solution 1:

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;
   returnthis;
};

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.

A note on best practices

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.

Post a Comment for "No "function.method" In Javascript?"