I am reading a Javascript book: Good Details. I am a little confused when I read the code below:
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; Number.method('integer',function(){ return Math[this < 0 ? 'ceil' : 'floor'](this); });
I think the first part of the above code means that any JavaScript function now has a method called a method. But is the "number" also a function? Why Number.method meaning of Number.method make sense?
I assume that Number inherits from Number.prototype, which inherits from Object.prototype (Number-> Number.prototype-> Object.prototype), since Number does not have a βmethodβ method at the beginning, it will search for it by prototype chain. But Function.prototype is not in the chain, right?
What is the relationship between Number, Number.prototype and Function.prototype?
UPDATE I:
I was looking for more information and now more confused. Some say Number is actually a function, and this seems to make sense because the value of Number instanceof Function is true . But the value (-10 / 3) instanceof Number is false . Doesn't that bother you? If the number in mathematics (for example, 3, 2.5, (-10/3)) is not even Number in JavaScript, how (-10 / 3) call integer() , which is a method from Number ? (The line below is in the same book)
document.writeln((-10 / 3).integer());
UPDATE II:
The problem is solved, basically.
Thanks to @Xophmeister help, I have now come to the conclusion that Number can call method because Number is a constructor, so it is associated with Function.prototype . As to why the number (3, 2.5, (-10/3)), whose type is a primitive type in JavaScript, can call a method that has a Number object, you should refer to this page .
I got this conclusion mainly from @Xophmeister help and a little search so that it is not accurate enough. Any corrections or additions are welcome.