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;
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.
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.
Baca Juga
- What Is Missing From This Description For Nested Functions And Closures At Mozilla Developer Network?
- In Javascript V8 Does Compilation Phase Happen To Functions Before Execution Phase Then All The Code Is Executed Or Only For Global Context
- How Does The Outer Function "store" The Arguments For The Inner Function?
Post a Comment for "No "Function.method" In JavaScript?"