Is everything related to the function prototype?

I am reading JavaScript. Good parts without prior knowledge of js, and this bit baffled me. I think I need an explanation.

JavaScript allows you to extend the basic types of language. In chapter 3, we saw that adding the Object.prototype method makes this method available to all objects. This also works for functions, arrays, strings, numbers, regular expressions, and Booleans. For example, adding Function.prototype, we can make the method available to all functions:

The following is an example:

Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; 

Now each base type has a method called method, so new functions can be defined for them, for example:

 Number.method('integer', function () { return Math[this < 0 ? 'ceiling' : 'floor'](this); }); 

But the book previously noted that all references to Object are not to Function! How it works?

+4
source share
1 answer

No, only functions have Function.prototype . Number is a constructor function, so it is β€œassociated” with this prototype.

Here is what the nodejs / V8 shell says about Number :

 > Number [Function: Number] > typeof Number 'function' 
+3
source

Source: https://habr.com/ru/post/1410845/


All Articles