Start with a function declaration

Declare a function object, instantiate it with new, and print

function person() {
	this.sex = "girl";
}
person.prototype.name = 'prototypeName';
var instance = new person();
console.dir(instance);

A normal object instantiated from the constructor

The following is displayed when you use Chrome

As you can see from the output, the current object is a normal object with a sex attribute defined and an internal __proto__ attribute automatically generated, which is an object type pointing to the prototype constructor person.

__proto__ is an internal property of the instantiated object (js uses the property to find the prototype chain internally, only FF and Chrom can access it)

Prototype is the internal property of the function object

Decompose the prototype of the Person constructor to which __proto__ points

When __proto__ is expanded, the following image appears:

As you can see from the figure above, the Person prototype consists of three properties,

  1. A name is a custom Person stereotype property;

  2. Constructor points to the Person constructor;

  3. __proto__ refers to the prototype of the Object constructor

Decompose the Person function object to which constructor points

  • From the picture above you can see, commonly used to initialize the arguments of the pseudo array parameter, the number of parameters defined length, and the names of the person, or object has its own prototype prototype at the same time, with the instantiation of the instance of __proto__ to equal;

  • The Function object also has a __proto__ attribute that points to the prototype of the Function constructor Function, whose prototype is the Function object.

From the above we can see some of the methods that are often used: The apply,bing, and call methods are defined in function. prototype. The Function object instantiated by Function has a __proto__ attribute pointing to the prototype object, so these methods are accessible from the Function object

Decompose the Function constructor object

Function, as a constructor that generates Function objects, is also a constructor that generates itself. It can print Function objects as follows:

Prototype refers to a Function object. Person. prototype is executed as a normal object. __proto__ is equal to function. prototype

Function.prototype.constructor === Function //true
Function.__proto__ === Function.prototype //true

Decompose the Object function Object

The Object Function Object is generated by the Function constructor, and you can use Object to generate normal objects used by javascript

In the picture above you can see to some of the commonly used static methods such as keys, create, defineProperty etc, including __proto__ pointing to the Function prototype; The prototype property is used for all object inheritance

Object.__proto__ === Function.prototype // true
Object.prototype.__proto__  //null

Summarize the structural relations of functions in a diagram

// red chain instance.__proto__ === person. Prototype; person.prototype.__proto__ === Object.prototype; // Purple chain object.prototype. __proto__ === null; Person.__proto__ === Function. Prototype; Function.__proto__ === Function.prototype; Function.prototype.__proto__ === Object.prototype; __proto__ === Function. Prototype;