What is the default prototype for a custom function in JavaScript?

function f() { } alert (f.prototype); // returns something like [object Object] 

In my opinion, by default, the prototype of a user-defined function should be null or undefined, can someone shed some light? thanks!

See also: How does __proto__ differ from the .prototype constructor?

+7
javascript prototype
source share
3 answers

The prototype object property is automatically created, it's just an empty object with the attributes of the {DontEnum} and {DontDelete} , you can see how function objects are created in the specification:

Pay attention to steps 9, 10 and 11:

9) Create a new object that will be created by the expression new Object() .

10) Set the property of the Result (9) constructor to F. This property has the { DontEnum } attributes.

11) Set the prototype property F for the result (9). This property has the attributes specified in 15.3.5.2 .

You can see that this is true:

 function f(){ //... } f.hasOwnProperty('prototype'); // true, property exist on f f.propertyIsEnumerable('prototype'); // false, because the { DontEnum } attribute delete f.prototype; // false, because the { DontDelete } attribute 
+6
source share
+3
source share

It is not undefined because you just defined it. Just because your function f() object is still empty does not mean that it is not defined. He simply determined that he had no content.

0
source share

All Articles