__proto__ functions

If I have a class called Person.

var Person = function(fname, lname){ this.fname = fname; this.lname = lname; } Person.prototype.mname = "Test"; var p = new Person('Alice','Bob'); 

Now p.__proto__ refers to the Person prototype, but when I try to do Person.__proto__ , it points to function() , and Person.constructor points to function() .

Can someone explain what is the difference between function() and function() and why function() is the prototype of function() ?

+4
source share
2 answers

Can someone explain what is the difference between functions () and Function () and why the function () is the prototype of the Function () class?

__proto__ is an implementation detail revealing [[prototype]]. The prototype [[prototype]] and the constructor need not be (often not) the same thing. Anyway...

Consider this hypothesis: This is impl. parts that depend on the engine — and in the particular engine under test (FF, which version?), Function is an object that itself has [[prototype]] functions. a function is a primitive function object. Person.prototype (by default) type functions (primitive object-function) and statements formulated as a result of this obvious dichotomy. (JS has some quirks: new Number(0) doesn't match 0 )

However, this is not the case in IE (8). In IE, the default prototype is a "simple object", not a function object.

+2
source

When defining such a function:

 var Person = function (fname, lname){ this.fname = fname; this.lname = lname; } 

This will make the Person function. A function is an object that must be built like any other "first class" object, so it has a constructor: the constructor of all function objects, called Function .

The prototype of all functions is an object named Function .

I like to refer to a good explanation by Mike Kos , which taught me a lot.

+1
source

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


All Articles