Prototype and closure are the difficulties of JS language, this article mainly talk about prototypes.
Each of these methods has a property that is prototype
Each object has a property that is _proto_
Once a prototype attribute or prototype method is defined, all objects instantiated through the constructor inherit these prototype properties and prototype methods, which are implemented through an internal _proto_ chain.
/* JS All functions have a prototype attribute that references an object, the prototype object, or a prototype. This function includes constructors and normal functions, and we are talking more about the prototype of the constructor, but we cannot deny that the common function also has a prototype.
Each method has a property called prototype (prototype)
Prototype definition: There is no need to explicitly declare a prototype property because it exists in every constructor. */
For example, the normal function:
function Test () {
}
Console.log ("EXP1:" + test.prototype); [Object Object]
Console.log ("EXP1:" + test.prototype instanceof Object)//true
Constructors, for example, construct objects. The first step is to understand the process of instantiating an object through a constructor function.
function A (x) {
This.x=x;
}
var obj=new A (1);
There are three steps to instantiating an obj object:
1. Create obj Objects : Obj=new object ();
2. to point the internal __proto__ of obj to the prototype of the construct of his function a, obj.constructor.prototype with the internal _proto_ is a different thing, the object is instantiated with _proto_, OBJ does not have a prototype attribute, but has an internal __proto__ that obtains prototype properties and prototype methods on the prototype chain through __PROTO__
3. use obj as this to invoke constructor A to set the members (that is, object properties and Object methods) and initialize them .
once a prototype attribute or prototype method is defined, all objects instantiated through the constructor inherit these prototype properties and prototype methods, which are implemented through an internal _proto_ chain .
For example: a.prototype.say=function () {alert ("Hi")};
all of the objects of a have a say method, and the say method of the prototype object is a unique copy for everyone to share, not every object has a copy of the Say method .
Here are a few examples for a deep understanding of prototype:
/* exp2:start...*/
/*
Adding attributes to Prototype
Prototype is an object, so you can add properties to it.
The property you add to prototype will be the common property of the object created using this constructor. */
function Fish (name, color) {
THIS.name = name;
This.color = color;
}
Fish.prototype.livesIn = ' water ';
Fish.prototype.price = 20;
Structure
var fish1 = new Fish (' Mackarel ', ' Gray ');
var fish2 = new Fish (' Goldfish ', ' orange ');
var fish3 = new Fish (' Salmon ', ' white ');
for (var i = 1; I <= 3; i++) {
var fish = eval (' Fish ' + i); Get a pointer to this fish
Console.log ("EXP2:" + fish.name + ', ' + Fish.color + ', ' + Fish.livesin + ', ' + fish.price);
}
Mackarel,gray,water,20
Goldfish,orange,water,20
Salmon,white,water,20
/* exp2:end...*/
/* exp3:start...*/
/* When an object is created, this constructor assigns its property prototype to the internal property __proto__ of the new object. This __proto__ is used by this object to find its properties. */
function Employee (name, salary) {
THIS.name = name;
This.salary = salary;
}
/* All objects of the employee have the Getsalary () and Addsalary () methods, both of which are unique and shared by this prototype object.
All objects that are not employee have this method (not one by one correspondence, but many to one) */
Employee.prototype.getSalary = function getsalaryfunction () {
return this.salary;
}
Employee.prototype.addSalary = function Addsalaryfunction (addition) {
return this.salary = this.salary + addition;
}
var boss1 = new Employee (' Jon ', 200000);
var boss2 = new Employee (' Kim ', 100000);
var boss3 = new Employee (' Sam ', 150000);
Console.log ("EXP3:" + boss1.getsalary ()); Output 200000
Console.log ("EXP3:" + boss2.getsalary ()); Output 100000
Console.log ("EXP3:" + boss3.getsalary ()); Output 150000
Console.log ("EXP3:" + boss1.addsalary (5000)); Output 205000
Console.log ("EXP3:" + boss2.addsalary (5000)); Output 105000
Console.log ("EXP3:" + boss3.addsalary (5000)); Output 155000
/* exp3:end...*/
/* exp4:start...*/
function F () {}
var i = new F ();
Console.log ("Exp4:i.prototype:" + I.prototype + ", f.prototype:" + F.prototype, ", i.__proto__:" + i.__proto_ _ + ", f.__proto__:" + F. __proto__);
exp4:i.prototype:undefined, f.prototype: [Object Object], i.__proto__: [Object Object] , f.__proto__: function Empty () {}
/* exp4:end...*/
Simple understanding of JavaScript prototype prototype