补基础之javascript面向对象-构造函数的继承

一、 构造函数绑定

  • 使用call或apply方法,将父对象的构造函数绑定在子对象上
function Animal(){
    this.species = "动物";
}
function Cat(name,color){
    Animal.apply(this, arguments);
    this.name = name;
    this.color = color;
}
var cat1 = new Cat("大毛","黄色");
console.log(cat1.species);
console.log(Cat.prototype);
console.log(Cat.constructor);
console.log(cat1.constructor);

二、 prototype模式

  • 将子对象(Cat)的prototype对象,指向父对象(Animal)的实例
  • 任何一个prototype对象都有一个constructor属性,指向它的构造函数(此例为Cat),但是因为第一行把Cat的prototype原型对象指向了Animal,所以需要手动改回Cat,否则会导致继承链的紊乱,这是很重要的一点,编程时务必要遵守
  • 即替换了如果替换了prototype原型对象,下一步必然是为新的prototype对象加上constructor属性,并将这个属性指回原来的构造函数
function Animal(){
    this.species = "动物";
}
function Cat(name,color){
    this.name = name;
    this.color = color;
}
Cat.prototype = new Animal();   //完全删除了prototype 对象原先的值,然后赋予一个新值。
console.log(Cat.prototype.constructor) //Animal
Cat.prototype.constructor = Cat;  //防止继承链紊乱
console.log(Cat.prototype.constructor); //Cat
var cat1 = new Cat("大毛","黄色");
console.log(cat1.species);  //动物
console.log(cat1.constructor);  //Cat

三、 直接继承prototype

  • 第三种方法是对第二种方法的改进。由于Animal对象中,不变的属性都可以直接写入Animal.prototype。所以,我们也可以让Cat()跳过 Animal(),直接继承Animal.prototype。
  • 与前一种方法相比,这样做的优点是效率比较高(不用执行和建立Animal的实例了),比较省内存。缺点是 Cat.prototype和Animal.prototype现在指向了同一个对象,那么任何对Cat.prototype的修改,Animal.prototype也跟着修改。
function Animal(){ }
Animal.prototype.species = "动物";
function Cat(name,color){
    this.name = name;
    this.color = color;
}
Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛","黄色");
console.log(cat1.species); // 动物
Cat.prototype.weight = 32;  //给Cat的原型添加属性
console.log(Cat.prototype)      //多了weight属性
console.log(Animal.prototype)   //多了weight属性

四、 利用空对象作为中介
- 跟直接继承差不多,只是多了个空对象作为中介,空对象几乎不占内存

function Animal(){}
Animal.prototype.species = "动物";
function Cat(name,color){
    this.name = name;
    this.color = color;
}
var F = function(){};
F.prototype = Animal.prototype;
Cat.prototype = new F();
Cat.prototype.constructor = Cat;
Cat.prototype.weight = 32;
var cat = new Cat('星星','黑白');
console.log(cat.species);
console.log(Animal.prototype.constructor);
console.log(Cat.prototype.constructor);

//继承方法的封
function extend(Child, Parent) {
  var F = function(){};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.uber = Parent.prototype;
  //意思是为子对象设一个uber属性,这个属性直接指向父对象的prototype属性。这等于在子对象上打开一条通道,可以直接调用父对象的方法。这一行放在这里,只是为了实现继承的完备性,纯属备用性质。
}

五、 拷贝继承

  • 把父对象的所有属性和方法,拷贝进子对象
  • -
function Animal(){}
Animal.prototype.species = "动物";
function Cat(name,color){
    this.name = name;
    this.color = color;
}
//封装拷贝继承方法
function extend(Child, Parent) {
    var p = Parent.prototype;
    var c = Child.prototype;
    for (var i in p) {
        c[i] = p[i];
    }
    c.uber = p;
}
extend(Cat, Animal);
var cat = new Cat("大毛","黄色");
console.log(cat.species);
Cat.prototype.weight = 32
console.log(Cat.prototype)
console.log(Animal.prototype)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值