I am going through JS the good parts and come to this example. In the last line I attempt to call upon a method that is defined in the prototype chain of the sum function. I am confused as to why this does not work.
Define the sum function:
var sum = function(){
var i, sum=0;
for(i=0;i<arguments.length;i+=1){
sum += arguments[i];
}
return sum;
}
add the method method to the function prototype
Function.prototype.method = function(name, func){
this.prototype.name = func;
}
use the method method to add an 'add' method to sum
sum.method('add', function(a,b){
return a + b;
})
The prototype method is clearly defined but I can not access it via the prototype chain:
sum.prototype.add(5,5); //returns 10
sum.add(5,5); //TypeError: sum.add not defined
In the last line why can I not access the method defined in the prototype chain of the sum function?