Introduction and summary of the relationship between Prototype, __proto__, Constructor, Object, and Function in Js

Summary: 1.js objects have internal properties [[prototype]] pointing to their prototype objects. [[prototype]] is called the prototype property. 2 Internal properties cannot be accessed directly. Error: obj[[prototype]], but can indirectly access [[prototype]]

Prototype
1 js objects have internal properties [[prototype]] pointing to their prototype objects. [[prototype]] is called the prototype property.

2 Internal properties cannot be accessed directly. error: obj[[prototype]], but can be accessed indirectly [[prototype]]

a ECMA: standard object prototype accessor Object.getPrototype(object) (so far only Firefox and chrome have implemented this accessor);

b non-standard accessor: __proto__ (except IE)

c obj.constructor.prototype

js everything is an object, so the function is also an object. And because the object has the internal property [[prototype]], so the
function also has Internal property [[prototype]].

Second: the function has both properties prototype. The prototype property and [[prototype]] are not the same property.

The prototype property points to an object, called the prototype object. So: the prototype property of a function (function's prototype property) has nothing to do with the actual prototype of the function (prototype) The role of the

prototype object: When the function fn is used as a constructor, the internal properties of all instances created by it [[prototype] ] points to the prototype property of fn.

The role of prototype:

1 Building prototype chain

When an object calls a method, if the method itself does not exist, it will look for its prototype until the top of the

prototype chain. The role of the prototype chain: methods required by multiple instances can be It is extracted and put into the prototype, so that it only needs to be defined once, and all objects that implement the shared method of multiple objects

Constructor
JS have the constructor property, pointing to its constructor. The prototype object of a function is also an object, so the prototype object also has the constructor property. And JS sets the rules:

fn.prototype.constructor == fn;
that is: the constructor field of the prototype property of the function points to the owner of the current prototype property, that is, the constructor itself Deduced

by fn.prototype.constructor == fn; The conclusion: the constructor attribute of the instance of the constructor points to its constructor

Deduction :

var a = new fn();
First look for a itself: the constructor attribute is not found. Then look up the prototype chain to find the prototype of a, that is fn.prototype, found the constructor property. So it is equivalent to the constructor property of the instance of the constructor always points to its constructor

Prototype, __proto__ and Object, Function relationship Introduction
Function, Object: The function object that comes with Js

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 property)

proto: each object has an internal hidden property named __proto__ , which points to its corresponding prototype object (named __proto__ in chrome and firefox, and can be accessed). The prototype chain is formed based on __proto__ (note: not based on the property prototype of the function object).

Regarding the function objects mentioned above, let's look at the following examples to illustrate:

var o1 = {};
var o2 = new Object();
function f1(){}
var f2 = function(){}
var f3 = new Function ('str','console.log(str)');
f3('aabb'); // aabb
console.log('typeof Object:'+typeof Object); //function
console.log('typeof Function: '+typeof Function); //function
console.log('typeof o1:'+typeof o1); //object
console.log('typeof o2:'+typeof o2); //object
console.log('typeof f1:'+typeof f1); //function
console.log('typeof f2:'+typeof f2); //function
console.log('typeof f3:'+typeof f3); // function
Usually we think that o1 and o2 are objects, that is, ordinary objects; f1, f2, and f3 are functions. But in fact, functions are also objects, which are constructed by Functions. The way f3 is written is the same as the way to create objects. In the end, f1 and f2, like f3, have functions constructed by the Function function.
f1, f2, and f3 are function objects. Function and Object themselves are also function objects

. Each object (except null) in Js is associated with another object. Analyze the relationship between Function, Object, Prototype, and __proto__ objects through the following examples and memory renderings
http://click.aliyun.com/m/23740/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640266&siteId=291194637