No "Function.method" in JavaScript? -


i reading book douglas crockford, , uses construct of

function.method('inherits', function(parent){    this.prototype=new parent();    return this; }); 

if leave alone meaning of it, can't around syntax. try run in chrome, ,

uncaught typeerror: undefined not function test3.html:18 (anonymous function) 

as happens if try (jsfiddle)

function.method("test", function () { return "test"; }); 

there seems post says line working, can't make work. why can be?

the reason line working in the post refer to because function.prototype has been extended method:

function.prototype.method = function (name, func) {    this.prototype[name] = func;    return this; }; 

if run above code , run code have, work - or can change .method .prototype[name_here] = , work same.

a note on best practices

if going extend prototypes in day , age better use object.defineproperty ensure method not enumerable.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -