Home>

For prototype, each function object has an explicit prototype property, and each object of proto has an internal hidden property named _proto_.This article introduces the relationship between prototype and proto in javascript.

prototype, each function object has an explicit prototype property, which represents the prototype of the object (function.prototype function object is an exception,There is no prototype attribute).

__proto__:Each object has an internal hidden property named __proto__,Point to its corresponding prototype object (named __proto__ in chrome and firefox, and can be accessed). The prototype chain was formed based on __proto__

(note:not based on the property prototype of the function object).

Simply put:__proto__ is an internal prototype,prototype is the constructor prototype (constructors are actually functions)

The prototype of a constructor is an object

So what is a constructor?

To create an object,First there is an object constructor,Just like in php,To create an object,First there must be a class

The essence of a constructor is a function,The following question is:how to create an object through this constructor?

Answer:new

Constructors construct objects.

First, __proto__ of all constructors/functions point to function.prototype, which is an empty function

number .__ proto__ === function.prototype

//true

boolean .__ proto__ === function.prototype

//true

string .__ proto__ === function.prototype

//true

object .__ proto__ === function.prototype

//true

function .__ proto__ === function.prototype

//true

array .__ proto__ ===

function.prototype

//true

regexp .__ proto__ === function.prototype

//true

error .__ proto__ ===

function.prototype

//true

date .__ proto__ ===

function.prototype

//true

It shows that number and so on are all constructors,These constructors are actually an object of function. That is equivalent to var number=new function ();

There are 12 built-in constructors/objects in javascript (json was added in ES5). Here are 8 accessible constructors.The rest such as global cannot be accessed directly,arguments are only created by the js engine when a function is called,math, json exists as an object,No need for new. Their __proto__ is object.prototype. as follows

math .__ proto__ === object.prototype

//true

json .__ proto__ === object.prototype

//true

"All constructors/functions" mentioned above of course include custom ones.as follows

//function declaration

function person ()

{}

//function expression

var man

=

function ()

{}

console.log (person .__ proto__ === function.prototype)

//true

console.log (man .__ proto__ ===

function.prototype)

//true

What is this indicating?

All constructors come from function.prototype, even the root constructor object and the function itself. All constructors inherit the properties and methods of function.prototype.Such as length, call, apply, bind (es5).

function.prototype is also the only prototype whose typeof xxx.prototype is "function". The prototypes of the other constructors are all objects.as follows

console.log (typeof function.prototype)

//function

console.log (typeof object.prototype)

//object

console.log (typeof number.prototype)

//object

console.log (typeof boolean.prototype)

//object

console.log (typeof string.prototype)

//object

console.log (typeof array.prototype)

//object

console.log (typeof regexp.prototype)

//object

console.log (typeof error.prototype)

//object

console.log (typeof date.prototype)

//object

console.log (typeof object.prototype)

//object

Oh, it was mentioned above that it is an empty function,look at alert (function.prototype).

Knowing that __proto__ for all constructors (including built-in and custom) is function.prototype, who is __proto__ for function.prototype?

I believe I have heard that functions in JavaScript are also first-class citizens.How can this be reflected?as follows

console.log (function.prototype .__ proto__ ===

object.prototype)

//true

This shows that all constructors are also ordinary js objects, and you can add/remove attributes to the constructor and so on.It also inherits all methods on object.prototype:tostring, valueof, hasownproperty, etc.

Who is __proto__ of object.prototype?

object.prototype .__ proto__ ===

null //

true

Let me share a function, object, prototype, and __proto__ memory relationship diagram

  • Previous PHP + redis implements adding methods to handle voting
  • Next Surprises with WebKit and Safari in iOS9