Home  >  Article  >  Web Front-end  >  Detailed explanation of the usage and disadvantages of JavaScript prototype chain inheritance method

Detailed explanation of the usage and disadvantages of JavaScript prototype chain inheritance method

伊谢尔伦
伊谢尔伦Original
2017-07-20 15:28:503013browse

Prototype chain method

function Person(){
     this.name = 'Simon';
}
Person.prototype.say = function(){
     alert('My name is '+this.name);
}
function F2E(id){
     this.id = id;
     this.showId = function(){
          alert('Good morning,Sir,My work number is '+this.id);
     }
}
F2E.prototype = new Person();

var simon = new F2E(9527);
simon.say();
simon.showId();
alert(simon.hasOwnProperty('id')); //检查是否为自身属性

Next, follow the above example to understand the following js prototype chain concept:

The prototype chain can be understood as: each object in js There is a hidden __proto__ attribute. The __proto__ attribute of an instantiated object points to the prototype method of its class, and this prototype method can be assigned to another instantiated object. The __proto__ of this object needs to point to Its class forms a chain, that is, the sentence

F2E.prototype = new Person()

in the previous code is the key. When a js object reads a certain attribute, it will first search for its own attributes. If there are no attributes, it will then search for the attributes of the object on the prototype chain. In other words, the prototype chain method can be shared, which solves the problem of object impersonation and wasting memory.

Let’s talk about the shortcomings:
The shortcomings are obvious. Prototype chain inheritance means that parameters cannot be passed to the parent class when instantiating a subclass, which is why function Person is used in this example. () has no parameters, but is directly written as this.name="Simon". The following code will not achieve the expected results:

function Person(name){
     this.name = name;
}
Person.prototype.say = function(){
     alert('My name is '+this.name);
}
function F2E(name,id){
     this.id = id;
     this.showId = function(){
          alert('Good morning,Sir,My work number is '+this.id);
     }
}
F2E.prototype = new Person();

var simon = new F2E("Simon",9527);
simon.say();
simon.showId();

 
function Person(name){
     this.name = name;
}

Person.prototype.say = function(){
     alert('My name is '+this.name);
}

function F2E(name,id){
     this.id = id;
     this.showId = function(){
          alert('Good morning,Sir,My work number is '+this.id);
     }
}

F2E.prototype = new Person();  //此处无法进行传值,this.name或者name都不行,直接写F2E.prototype = new Person('wood')是可以的,但是这样的话simon.say()就变成了My name is wood

var simon = new F2E("Simon",9527);
simon.say();  //弹出 My name is undefined
simon.showId();

Finally, let’s summarize the inheritance implementation method that I think is better. Member variables use object impersonation, and member methods use prototype chaining. The code is as follows:

function Person(name){
     this.name = name;
}
Person.prototype.say = function(){
     alert('My name is '+this.name);
}
function F2E(name,id){
     Person.call(this,name);
     this.id = id;
}
F2E.prototype = new Person(); 
//此处注意一个细节,showId不能写在F2E.prototype = new Person();前面
F2E.prototype.showId = function(){
     alert('Good morning,Sir,My work number is '+this.id);
}
var simon = new F2E("Simon",9527);
simon.say();
simon.showId();


The above is the detailed content of Detailed explanation of the usage and disadvantages of JavaScript prototype chain inheritance method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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