three basic concepts: constructors, prototypes, instances
Each constructor has a prototype object (prototype), and the prototype object contains a pointer to the constructor (constructor), and the instance contains an internal pointer to the prototype (__PROTO__).
EMP1:
functionthis. y == tenfunctionreturnthis . x+this. y+z;}; var New Foo (+); alert (B.calculate (30));
The default prototype for all functions is an instance of object, so there is a pointer inside the default prototype that points to Object.prototype, which is why all custom objects inherit the root cause of methods such as ToString (), ValueOf ().
The constructor of Foo has values for the __proto__ of any one object, because each object has a constructor.
Functions and objects
Functions are objects, and objects are created by functions. There are prototype properties within any function.
In JavaScript, an object is a collection of any unordered key-value pairs, which is an object if it is not a primary data type (Undefined,null,boolean,number,or string).
False is obviously a primary data type, FALSE.__PROTO__ also returns a value. Because in code execution it is coerced into an object (the Boolean basic wrapper type).
Prototype wording
Step through: (with real-time)
function A () { this. Age = 1;} = ' ss '; var New a (); A.prototype.name= ' Tom '; Console.log (B.STR) ; // SS //
Literal notation: (will cause a prototype rewrite)
functionA () { This. Name = ' W '; } a.prototype.str= ' s '; varb =NewA (); Console.log (B.STR); //s //literal notationA.prototype ={x:1 }; //a prototype rewrite is not reflected in the instance created prior to aConsole.log (B.STR);//sConsole.log (b.x);//undefined //A's prototype is reflected in the instance created after the rewrite varc =NewA (); A.PROTOTYPE.Q= 3; Console.log (c.x); //1Console.log (C.Q);//3Console.log (C.STR);//undefined
Prototype chain inheritance
The prototype of function A is an instance of function B, and the instance of function a inherits all the methods and properties of the prototypes of A and B. See figure "JavaScript Advanced Programming" p163
Constructor
Constructor refers to the object's constructor object, which is constructed by a function
functionvarnew Foo (); alert (foo.constructor) ; // alert (foo.constructor); // alert (object.constructor); // alert (function.constructor); //
Because foo,object,function are both function objects, and because all function objects are function objects, they are constructor as functions.
The relationship between prototype and constructor
function Dog () {}
= = = Dog.prototype.constructor); //
Console.log (Dog.prototype.hasOwnProperty (' constructor ')); True
function Animal () {}
function person () {}
var person = new person ();
Console.log (person.constructor); Person
Any prototype of a function has the constructor property
Person is an instance of person, so the person inherits all the properties and methods on the person prototype, the Preson prototype has the constructor property and points to Preson, so the person has a constructor attribute and points to Preson
Go a little deeper
functionfunctionnew varnew//
The dashed line in the figure indicates that the person's default prototype point (for reference only). But we're pointing person.prototype to new Animal.
And also
hasOwnProperty instanceof isprototypeof Function prototype attribute value contains the application type value and so on
JavaScript Prototyping Learning Notes