I started to code in Javascript early this year. I am having hard time figuring out some of Javascript key concepts, especially the prototypal inheritance. I learned from Douglas Crockford's book that if you augment Function.prototype, you can make a method available to all functions. And, there comes this code...
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
I tested the following code from the book and it works.
String.method('trim',function(){
return this.replace(/^\s+|\s+$/g, '');
});
console.log('"'+' neat '.trim() + '"');
Then, I tried to create a custom function and augment a method to it.
function Foo()
{
};
Foo.method('test', function() {
return "This is a test!";
});
console.log(Foo.test());
actually, I tried console.log different combinations Foo, test(), proto, etc., but I just couldn't get "This is a test!" printed out. Could anyone help?