Home > Web Front-end > JS Tutorial > Some problems with prototypal inheritance

Some problems with prototypal inheritance

一个新手
Release: 2017-09-06 11:48:59
Original
1100 people have browsed it

When we use prototype chain inheritance, we need to be careful in defining methods and properties on the prototype, because this may bring unexpected results.

1. Define methods on the prototype carefully.
When we want to define a method on the prototype of a constructor, we must define it after changing the prototype, otherwise the new prototype object will not have this method defined, resulting in the results we expected. different. Example:


1 function superObj(){}
2 superObj.prototype.sayHi=function sayHi(){
3     console.log('hi');
4 };
5 superObj.prototype={
6 name:'Poly'
7 };
8 var obj=new superObj();
9 obj.sayHi();//报错!! superObj.sayHi is not a function
Copy after login

The correct operation is as follows


1 function superObj(){}
2 superObj.prototype={
3     name:'Poly'
4 };
5 superObj.prototype.sayHi=function sayHi(){6     console.log('hi');7 };8 var obj=new superObj();9 obj.sayHi();// 'hi'
Copy after login

2. Do not use object literals to create properties/methods for the prototype.
Using object literals will create a new object and assign the reference address of the new object to the prototype of the constructor. Example


1 function superObj(){}
2 superObj.prototype={
3     sayHi:function sayHi(){
4         console.log('hi');
5     }
6 }
Copy after login

The correct operation is as follows:


1 function superObj(){}
2 superObj.prototype.sayHi=function sayHi(){
3     console.log('hi');
4 }
Copy after login

3. There is a direct correspondence between object instances and prototypes.
This means that when a reference to __proto__ is made, the reference address of the prototype will be saved. Even if the prototype of the constructor changes, it will not affect the __proto__ in the previously created instance. Example


 1 function superObj(){} 
 2 superObj.prototype.say=function() { 
 3     console.log('hello'); 
 4 } 
 5 var obj=new superObj();
 6 superObj.prototype={ 
 7     say:function() { 
 8         console.log('world'); 
 9     }
 10 };
 11 var obj2=new superObj();
 12 obj.say();//'hello'
 13 obj2.say();//'world'
Copy after login

Fourth, it is best not to define attributes with reference types on the prototype.
If a property with a value of a reference type is defined on the prototype, then all instances will share the property value (reference type value, pointing to the same object), and when one of the instances modifies the value or property on the reference type , the changes will occur on all instances. Therefore, properties whose values ​​are reference types are best defined in the constructor. Example


1 function superObj(){}
2 superObj.prototype.ary=[1,2,3];
3 var obj1=new superObj();
4 var obj2=new superObj();
5 obj1.ary[0]=0;//obj1.ary和obj2.ary指向的是同一个数组,当obj1修改此数组时,obj2.ary也会发生改变
6 console.log(obj2.ary[0]);//0
Copy after login
<br/>
Copy after login

If you don’t want instances to share the same reference object, you should define it in the constructor. Example

<br/>

1 function superObj(){
2     this.ary=[1,2,3];
3 }
4 var obj1=new superObj();
5 var obj2=new superObj();
6 obj1.ary[0]=0;//obj1.ary和obj2.ary指向的不是同一个数组,所以修改obj1.ary不会影响obj2.ary
7 console.log(obj2.ary[0]);//1
Copy after login

The above is the detailed content of Some problems with prototypal inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template