JavaScript是如何实现继承的(六种⽅式)
前⾔:⼤多OO语⾔都⽀持两种继承⽅式:接⼝继承和实现继承,⽽ECMAScript中⽆法实现接⼝继承,ECMAScript只⽀持实现继承,⽽且其实现继承主要是依靠原型链来实现。
1.原型链
基本思想:利⽤原型让⼀个引⽤类型继承另外⼀个引⽤类型的属性和⽅法。
构造函数,原型,实例之间的关系:每个构造函数都有⼀个原型对象,原型对象包含⼀个指向构造函数的指针,⽽实例都包含⼀个指向原型对象的内部指针。
原型链实现继承例⼦:
function SuperType() {
this.property = true;
}
SuperValue = function() {
return this.property;
}
function subType() {
this.property = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
SubValue = function (){
return this.property;
}
var instance = new SubType();
console.SuperValue());//true
2.借⽤构造函数
基本思想:在⼦类型构造函数的内部调⽤超类构造函数,通过使⽤call()和apply()⽅法可以在新创建的对象上执⾏构造函数。
例⼦:
function SuperType() {
}
function SubType() {
SuperType.call(this);//继承了SuperType
}
var instance1 = new SubType();
console.lors);//"red","blue","green","black"
var instance2 = new SubType();
console.lors);//"red","blue","green"
3.组合继承
基本思想:将原型链和借⽤构造函数的技术组合在⼀块,从⽽发挥两者之长的⼀种继承模式。
例⼦:
function SuperType(name) {
this.name = name;
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name, age) {
SuperType.call(this,name);//继承属性
this.age = age;
}
//继承⽅法
SubType.prototype = new SuperType();
structor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
consol.lors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.lors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建⾃定义的类型。
原型式继承的思想可⽤以下函数来说明:
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
例⼦:
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
ECMAScript5通过新增ate()⽅法规范化了原型式继承,这个⽅法接收两个参数:⼀个⽤作新对象原型的对象和⼀个作为新对象定义额外属性的对象。
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = ate(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = ate(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
5.寄⽣式继承
基本思想:创建⼀个仅⽤于封装继承过程的函数,该函数在内部以某种⽅式来增强对象,最后再像真正是它做了所有⼯作⼀样返回对象。
例⼦:
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
};
return clone;
}
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"
6.寄⽣组合式继承
js原型和原型链的理解基本思想:通过借⽤函数来继承属性,通过原型链的混成形式来继承⽅法
其基本模型如下所⽰:
function inheritProperty(subType, superType) {
var prototype = object(superType.prototype);//创建对象
subType.prototype = prototype;//指定对象
}
例⼦:
function SuperType(name){
this.name = name;
}
SuperType.prototype.sayName = function (){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}
以上内容给⼤家介绍了javascript实现继承的六种⽅式,希望对⼤家有所帮助!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论