Chenjiayuan195 / Summary-of-front-end-knowledge-points

1 stars 0 forks source link

JS:js继承的几种方式 #17

Open Chenjiayuan195 opened 4 years ago

Chenjiayuan195 commented 4 years ago

1.原型链继承

function People(){
}
People.prototype.getName = function(){
 console.log('people')
}

function Woman(){ 
}
Woman.prototype= new People();
Woman.prototype.constructor  = Woman ;
let womanObj = new Woman();

2.原型式继承

function People(){
}
People.prototype.getName = function(){
 console.log('people')
}

function Woman(){ 
}
Woman.prototype = People.prototype
let womanObj = new Woman();

2.借用构造函数继承

function People(){
}
People.prototype.getName = function(){
 console.log('people')
}

function Woman(){
 People.call(this);
}

3.组合式继承

function People(){
}
People.prototype.getName = function(){
 console.log('people')
}

function Woman(){
 People.call(this);
}
Woman.prototype = People.prototype
let person = new Woman()