借用构造函数和组合继承

6.3.2借用构造函数
用来解决原型中包含引用类型所带来的问题。
解决思路:在子类型的函数构造的内部调用超类型的构造函数

function Person7 () {
    this.colors = ['天下','小','晓晓'];
}

function Son7() {
    //继承了Person7
    //这里通过改变this的指向,并且调用了构造函数。
    Person7.call(this);
}

var instance1 = new Son7();
instance1.colors.push('aa123');
console.log('instance1',instance1.colors);

var instance2 = new Son7();
console.log('instance2',instance2.colors);
Person7.call(this)

这行代码借调了超类型的构造函数。通过Call方法和apply方法,我们实际在Son7实例化的环境下调用了构造函数Person7。这样一来就会在新Son7对象上执行Person7()函数中定义的所有对象初始化代码,结果每个Son7的每个实例都会有自己的colors属性的副本了。

//1.传递参数
function Person8(name) {
    this.name = name;
}

function Son8() {
    Person8.call(this,'天下');
    this.age = 18;
}
var son8 = new Son8();
console.log('son8',son8);
console.log('son8.age',son8.age);

借用构造函数存在的问题:

  • 方法都在构造函数中定义,无法复用。
  • 在超类型的原型中定义的方法,对自类型而言是不可见的。
    6.3.3 组合继承
    也叫伪经典继承,指的是将原型链和借用构造函数的技术组合在一块,从而发挥二者之长的一种继承模式。
    思想:使用原型链实现对原型属性和方法的继承,而通过构造函数来实现对实例属性的继承。
function Person13(name) {
    console.log('name',name);
    this.name = name;
    this.colors = ['aa','bb'];
}
//共享的方法
Person13.prototype.sayName = function() {
    return this.name;
}

function Son13(name,age) {
    //继承属性
    Person13.call(this,name);
    this.age = age;
}

Son13.prototype = new Person13();
var son13  = new Son13('秦扫六合',1);
Son13.prototype.sayAge = function() {
    return this.age;
}
son13.colors.push('123');

console.log('son13.colors',son13.colors);
//原型方法是共享的
console.log('son13.sayName',son13.sayName()); //天下
console.log('son13.sayAge',son13.sayAge());
console.log('son13.age',son13.age);

var son131 = new Son13('统一天下',2);
console.log('son131.colors',son131.colors);
console.log('son131.age',son131.age);

//原型方法是共享的
console.log('son131.sayName',son131.sayName()); //天下
console.log('son131.sayAge',son131.sayAge());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
原型链继承(Prototype Inheritance)在JavaScript中是通过创建一个新对象并让它引用另一个对象的原型来实现的。例如: ```javascript function Parent() {} Parent.prototype.method = function() { console.log('Parent method'); }; let child = new Parent(); child.method(); // 输出: "Parent method" ``` **借用构造函数继承**(Constructo r Chaining)利用已有构造函数作为父类,通过`new`关键字传递给子类实例化过程,间接实现了继承: ```javascript function Parent() { this.parentProp = 'parent'; } function Child() { Parent.call(this); // 借用父类构造函数 this.childProp = 'child'; } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; let childInstance = new Child(); console.log(childInstance.parentProp); // 输出: "parent" console.log(childInstance.childProp); // 输出: "child" ``` **组合继承**(Mix-in or Prototype Mixing)结合原型链和构造函数继承,允许从多个源继承属性和方法: ```javascript function Mixin(target) { for (let prop in Mixin.prototype) { target[prop] = Mixin.prototype[prop]; } } function Parent() { this.parentProp = 'parent'; } Mixin(Parent.prototype); let child = new Parent(); console.log(child.parentProp); // 输出: "parent" ``` **ES6的class类继承**(Class-based Inheritance)使用`extends`关键字实现: ```javascript class Parent { constructor() { this.parentProp = 'parent'; } parentMethod() { console.log('Parent method'); } } class Child extends Parent { constructor() { super(); this.childProp = 'child'; } childMethod() { console.log('Child method'); } } let childInstance = new Child(); childInstance.parentMethod(); // 输出: "Parent method" childInstance.childMethod(); // 输出: "Child method" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值