Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default prototype for custom function in JavaScript?

function f()
{
}

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

My understanding is by default the prototype of custom function should be null or undefined, can someone shed some light? thanks!

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

like image 793
nandin Avatar asked Feb 26 '10 20:02

nandin


People also ask

What is default prototype?

prototype, constructor property. Every function has the "prototype" property even if we don't supply it. The default "prototype" is an object with the only property constructor that points back to the function itself. Like this: function Rabbit() {} /* default prototype Rabbit.prototype = { constructor: Rabbit }; */

What is __ proto __ and prototype in JavaScript?

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new : ( new Foo ).

What does __ proto __ mean in JavaScript?

The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.

What is the prototype of the function?

A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).


1 Answers

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

  • 13.2 Creating Function Objects

Pay attention to the steps 9, 10 and 11:

9) Create a new object as would be constructed by the expression new Object().

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

11) Set the prototype property of F to Result(9). This property is given attributes as specified in 15.3.5.2.

You can see that this is true by:

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
like image 176
Christian C. Salvadó Avatar answered Oct 16 '22 10:10

Christian C. Salvadó