I want to extend the type ‘Number’ with a new function and hence I have to define a prototype. When I think about this, I get a bunch of questions:
- Does Number inherit from both Object.prototype as well as Function.prototype?
- Is ‘Number’ an ‘Object’ or a ‘Function’?
- When should I define an object as a prototype for Number? Does it make sense?
1- True.
Number instanceof Objectreturns true alsoFunction instanceof Objectreturns true. So Number has all methods that Object and Function has.2- Number is a function.
typeof Numberreturns “function”.3- If you want to add a method to Number’s prototype, just use
Number.prototype.METHOD_NAME = function() { // your logic }Then you can call your method on all numbers like
1..METHOD_NAME()