The difference between function and function.

According to this, functions inherit from Function and Function from Function.prototype in turn:

The global Function object has no methods or properties, however, since it is the function itself, it inherits some methods and properties through the prototype chain from Function.prototype .

What does Function.prototype point mean? Why not move its properties to Function and let Function.prototype be undefined ? Instead, functions will be obtained from Function .

The same goes for Object , etc.

+6
source share
3 answers

functions inherit from Function

You are quoting MDN freely. This actually says:

function objects inherit from Function.prototype

Note that on the MDN page, the initial word “function” in the sentence above is capitalized, but only because it is at the beginning of the sentence, and not because it refers to a JS Function object. This applies to the usual old functions declared as function() { } .

Remember that MDN is written by mere mortals. I would prefer that they do not use the words "inherit" and "inheritance", not to mention "received." JS has no concept of inheritance in the strict sense of the word. If you use this terminology, you will end up confused. JS has prototypes related to objects. When accessing the object of the object, if it is not found, consult the prototype. If it is not found, since the prototype is also an object with a prototype, a prototype prototype will be consulted, etc. Up the chain.

Therefore, the above sentence would be better written as "functional objects have Function.prototype as their prototype ."

The JS Function object is not directly associated with Function.prototype , except that Function.prototype is a property of Function , and since the Function object itself is a function, it itself has Function.prototype as its prototype. Regardless of what properties may or may not be present on Function or you hang on it, they have nothing to do with the prototype chain and are not "inherited" by anyone.

When you execute (function() { }).call() , the call property / method is first scanned on the function object itself; if it does not exist there, as would normally not be the case, then it is viewed on a prototype, assigned essentially when the function was declared, which is equal to Function.prototype . Where else could you put methods like call or apply , if not on Function.prototype ? What else could you call a prototype automatically assigned to functions other than Function.prototype ?

Note that Function.call correctly enable the internal call function. What for? Not because call lives there, or because it happens because regular functions “inherit” call , but rather because, as I mentioned earlier, Function itself is a function and therefore has a prototype Function.prototype , call can be found in the prototype chain.

What does Function.prototype point mean? Why not move your properties to Function and Function.prototype undefined? Instead, functions will be obtained from Function .

X.prototype is a property of X used as a prototype for objects created using X as a constructor. Therefore, to assign the correct prototype to objects created using Function as a constructor (which includes functions declared as function x() { } ), the prototype must be present as a prototype property on Function .

+6
source

Prototype functions are created only once and shared between each instance. Functions created in the constructor are created as new objects for each new object created using the constructor.

As a rule, functions should be on the prototype, as they, as a rule, will not be changed for different objects of the same type, and this has little benefit from memory / performance. Other properties, such as objects and arrays, must be defined in the constructor if you do not want to create a common static property, in which case you should use a prototype.

It’s easier to see differences with regular objects or arrays rather than functions

 function Foo(){ this.bar = []; } var fooObj1 = new Foo(); var fooObj2 = new Foo(); fooObj1.bar.push("x"); alert(fooObj2.bar) //[] as opposed to: function Foo(){ } Foo.prototype.bar = [] var fooObj1 = new Foo(); var fooObj2 = new Foo(); fooObj1.bar.push("x"); alert(fooObj2.bar) //["x"] 
+2
source

You need Function.prototype , if you want to extend functions, consider this:

 Function.prototype.moo = 5; function x() { }; console.log(x.moo); // 5 

each function you execute has the moo property, against:

 Function.quack = 6; function b() {}; console.log(b.quack); // undefined 

This is not true if you simply apply the property to Function . Each function does not inherit the properties assigned to Function , so you need Function.prototype .

0
source

All Articles