I am reading the "Javascript the Good Parts" book and trying out examples to understand the concepts. I came across one example and could not understand. Please look at the code below and let me understand where I am going wrong:
//Augmenting the Function prototype with method
Function.prototype.method = function(name, func){
if (typeof this.prototype[name] !== "function"){
this.prototype[name]=func;
return this;
}
}
// why do we have the (this) at the end of the return statement.
/*Number.method("integer", function(){
return Math[this < 0 ? 'ceil': 'floor'](this);
});*/
//According to me the function should have been like below:
Number.method("integer", function(val){ // we get a function and we need to pass the value and the value will be evaluated and returned.
return Math[val < 0 ? 'ceil': 'floor'];
});
//According to my understanding the calling function should be something like below.
alert((-10/3).integer(-10/3);
I know that my approach is not working but finding hard to get the reasoning. Please update me with some example to reinforce the concepts.
Sharing the link to the Fiddle - Fiddle - link