JavaScript是如何实现继承的(六种方式)

大多数语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现。

1.原型链

基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。

构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

<script>
    //定义猫的对象
    var kitty = { color: 'yellow', bark: function() { alert('喵喵'); }, climb: function() { alert('我会爬树') } };

    //老虎对象的构造函数
    function tiger() {
        this.color = "yellow and black";
        this.back = function() {
            alert('吼吼...');
        }
    }

    //给构造函数声明原型,那么构造出的对象,就会有一个祖先:即该原型
    tiger.prototype = kitty;
    //或 tiger.prototype = new kitty();//如果kitty为function,则采用这种方式

    var t = new tiger();
    document.write(t.color); //yellow and black
    t.climb(); //我会爬树
    //当调用老虎的属性或方法时,首先在其构造函数找;如果没有,则到老虎构造函数的原型。
    //但要注意,这里它并不是把原型对象里的climb()方法复制到自身。这就是原型链查找。
    //kitty也是有构造方法的,即new Object()。Object默认也有一些方法和属性。同时,它也有原型,只是为空而已 { }
</script>

2.借用构造函数

基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。

function SuperType() {
    this.colors = ["red","blue","green"];
}
function SubType() {
    SuperType.call(this);//继承了SuperType
}
var instance1 = new SubType();
instance1.colors.push("black"); //push()向数组末尾插入数据
console.log(instance1.colors);//"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"

3.组合继承

基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。

function SuperType(name) {
    this.name = name;
    this.colors = ["red","blue","green"];
}
SubType.prototype.sayName = function() {
    console.log(this.name);
}
function SubType(name, age) {
    SuperType.call(this,name);//继承属性
    this.age = age;
}
//继承方法
SubType.prototype = new SubType;
SubType.prototype.constructor = SubType();
SubType.prototype.sayAge = function() {
    console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"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通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。

var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"]
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(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");  //hi
};
return clone;
}
var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();

6.寄生组合式继承

基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法

其基本模型如下所示:

function inheritProperty(subType, superType) {
  var prototype = object(superType.prototype);//创建对象
  prototype.constructor = subType;//增强对象
  subType.prototype = prototype;//指定对象
}
function inheritPrototype(subType,superType){
    var prototype=Object.create(superType.prototype); //创建父类原型的一个副本 等同于使用Object.create(superType.prototype)
prototype.constructor=subType;   //为副本添加constructor属性,弥补重写原型而失去的constructor属性
subType.prototype=prototype; //将创建的对象(副本)赋值给子类的原型
}
function SuperType(name){
    this.name=name;
    this.friends=["gay1","gay2"];
}
SuperType.prototype.sayName=function(){
    alert(this.name); //nUll
};
function SubType(name,age){
    SuperType.call(this,name);  //继承SuperType
this.age=age;       //(扩展出age属性)
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge=function(){
    alert(this.age);//25
};//扩展出sayAge方法
var person1=new SubType("nUll",25);
var person2=new SubType("mywei",25);
person1.friends.push("gay3");
person1.sayName();
person1.sayAge();
alert(person1.friends);    //gay1,gay2,gay3
alert(person2.friends); //gay1,gay2
alert(person1 instanceof SubType);   //true
alert(person1 instanceof SuperType);  //true
alert(SubType.prototype.isPrototypeOf(person1));  //true
alert(SuperType.prototype.isPrototypeOf(person1)); //true

 

4.原型式继承

基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。

原型式继承的思想可用以下函数来说明:

1
2
3
4
5
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
posted @ 2017-04-13 23:15  爽朗琴天  阅读(106)  评论(0)    收藏  举报