Home>

This article details the internal prototype of javascript __proto__ and the prototype prototype of the constructor, and their similarities and differencesVery detailed,If you are in need, come and study it.

__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

Has reached the top,Is null.

  • Previous Method to get address bar parameters using JavaScript
  • Next Discover the new features of C # 60