The prototype
define
A prototype is a property of the function object, and the result of the console object is an object.
The sample code
function Handphone (color,brand){
this.color = color;
this.brand = brand;
this.screen = 'and';
this.system = 'Android';
}
Handphone.prototype.rom = '64G';
Handphone.prototype.ram = '6G';
var hp1 = new Handphone('red'.'millet');
console.log(hp1);
console.log(hp1.rom);
Copy the code
The results of
When using Prototype, we were able to access the ’64G’ printed from [hp1.rom]. That is to say,
This prototype is the common ancestor of every object constructed by the definition constructor.
All objects constructed by this constructor can inherit properties and methods from the stereotype.
In order to reduce the coupling degree of development, in fact, all methods will eventually be mounted to the prototype, for example, our above code,screen and system are written dead, in fact, this is not good, we try to rewrite:
Handphone.prototype.screen = '18: nine';
Handphone.prototype.system = 'Android';
Handphone.prototype.rom = '64G';
Handphone.prototype.ram = '6G';
Copy the code
Only the parts that need to be configured as parameters are written in the constructor, for example:
function Handphone (color,brand){
this.color = color;
this.brand = brand;
}
Copy the code
We can also add methods to the prototype:
Handphone.prototype.call = function(){
console.log('I'm on the phone');
}
// The same can be used:
hp1.call();
/ / access to
Copy the code
At the same time
console.log(Handphone.prototype);
Copy the code
If printed like this, the result would be {constructor f}. At this stage, we just need to remember that constructor refers to the constructor itself.
At this moment
// We can redirect the handphone constructor to a new function like this:
function Telephone(){}
Handphone.prototype = {
constructor : Telephone
}
Copy the code
When we print it out, the result will point to Telephone instead of Handphone.
Additional summary
Prototype belongs to every instantiated object, not to a constructor.
The “__ proto __:Car. Prototype “method is required by JS to emphasize that this is the same thing as the source file and should not be changed easily. It can be understood as the form of key-value pairs.
So,__ proto __ is the container for each instantiated object’s prototype, which is used to hold Prototype.
but
Prototype properties can also be changed, for example:
function Person(){}// Define a function
person.prototype.name = '233';
var P1 = {
name : 'www';
}
var person = new Person();
console.log (person.name); // This is 233
person.__proto__ = P1;
// Here we assign the p1 property to proto and overwrite the name property
console.log(person.name);// What is printed here is the rewritten WWW
Copy the code
About inheritance in stereotypes
p.prototype.tSkill = 'java';
function p (){}
var P = new p();
T.prototype = P;
function T (){
this.mSkill = 'JS';
}
var t = new T();
s.prototype = t;
function s(){
this.pSkill = 'css';
}
var S = new s();
console.log(S.pSkill);
Copy the code
Follow the proto line to find the properties in the prototype, and the chain that inherits the properties of the prototype layer by layer is called the prototype chain.
So the top of the prototype chain is object.prototype
Object. Prototype saves toString (); This method.
In the inheritance of the prototype chain, the next level is usually not able to add, delete or change, only to check. But it’s not absolute, for example:
p.prototype.tSkill = 'java';
function p (){}
var P = new p();
T.prototype = P;
function T (){
this.mSkill = 'JS';
this.sucess{
a:'28';
d:'30'; }}var t = new T();
s.prototype = t;
function s(){
this.pSkill = 'css';
}
var S = new s();
S.sucess.b = '100';
Sucess = sucess; sucess = sucess; sucess = sucess; sucess = sucess; sucess = sucess
console.log(S.pSkill);
Copy the code
So reference values are ok, but not recommended.
The create method
Syntax format: object.creat (); You can only put objects or null inside parentheses, you have to put things, you have to put things.
// Or we can do this:
var test = {
num:2
}
funtion Obj(){}
Obj.prototype.num = 1;
var obj1 = Object.create(Obj.prototype);
//var obj1 = Object.create(test); You can also pass in a test object.
var obj2 = new Obj();
// Either way, the result is the same.
console.log(obj1);
console.log(obj2);
Copy the code
Because of the CREATE method, here’s an interview question:
Do all objects inherit from Object.prototype?
// Create objects that do not inherit from Object.prototype
var obj1 = Object.create(null);
Copy the code
Can we add the proto attribute by ourselves? Please look at the following code:
var obj = Object.create(null);
obj.num = 1;
var obj1 = {
count:2
}
obj.__proto__ = obj1;
console.log(obj.count);
Copy the code
The final result is
undefined
Copy the code
That is to say, only the proTO added by the system can be executed normally. If it is added by ourselves, it will not take effect.
(The proto attribute of the system is displayed in a lighter color on the console, but we add it in a darker color.)