小结JS实现继承的几种方式

2019-07-31  本文已影响0人  繁络

js中的继承与其说是对象的继承,但更像是让函数的功能和用法的复用,组合;例如两个不同的函数各有各的方法,但是我想拥有两个函数所有的方法,就可以通过继承得到,下面总结了js实现继承的几种方式以及各自的优缺点:


首先定义一个父类

// 定义一个父类
function Father(name) {
    // 属性
    this.name = name || '->father';
    // 实例方法
    this.sayName = function() {
        console.log(this.name);
    }
}
// 原型方法
Father.prototype.age = 18;
Father.prototype.sayAge = function() {
    console.log(this.age);
}
准备好了么? 要开始了哦~

1、原型链继承:将父类的实例作为子类的原型

function Son(name) {
    // Father.call(this);
    this.name = name || '->son';
}
Son.prototype = new Father();
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // true
console.log(s instanceof Son); // true

2、构造继承:复制父类的实例属性给子类

function Son(name) {
    Father.call(this);
    this.name = name || '->son';
}
var s = new Son('son');
console.log(s.name); // son
//s.sayAge(); // 抛出错误
s.sayName(); // son
console.log(s.age); // undefined
console.log(s instanceof Father); // false
console.log(s instanceof Son); // true

3、实例继承:为父类实例添加新特征,作为子类实例返回

function Son(name) {
    var f = new Father();
    f.name = name || '->son'
    return f;
}
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // true
console.log(s instanceof Son); // false

4、拷贝继承:对父类实例中的的方法与属性拷贝给子类的原型

function Son(name) {
    var f = new Father();
    for(var k in f) {
        Son.prototype[k] = f[k];
    }
    Son.prototype.name = name || '->son';
}
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // false
console.log(s instanceof Son); // true

5、组合继承:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用

function Son(name) {
    Father.call(this);
    this.name = name || '->son'
}
Son.prototype = new Father();
// 修复构造函数指向
Son.prototype.constructor = Son;
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // true
console.log(s instanceof Son); // true

6、寄生组合继承:通过寄生方式,砍掉父类的实例属性,避免了组合继承生成两份实例的缺点

function Son(name) {
    Father.call(this);
    // var f = new Father();
    this.name = name || '->son'
}
(function() {
    // 创建一个没有实例方法的类
    var None = function() {};
    None.prototype = Father.prototype;
    // 将实例作为子类的原型
    Son.prototype = new None();
    // 修复构造函数指向
    Son.prototype.constructor = Son;
})();
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // true
console.log(s instanceof Son); // true

7、Class继承:使用extends表明继承自哪个父类,并且在子类构造函数中必须调用super

class Son extends Father {
    constructor(name) {
        super(name);
        this.name = name || '->father';
    }
}
var s = new Son('son');
console.log(s.name); // son
s.sayAge(); // 18
s.sayName(); // son
console.log(s.age); // 18
console.log(s instanceof Father); // true
console.log(s instanceof Son); // true
上一篇 下一篇

猜你喜欢

热点阅读