JS之继承

// 定义一个动物类
function Animal (name) {
    // 属性
    this.name = name || 'Animal';
    this.share = [123]
    // 实例方法
    this.sleep = function(){
        console.log(this.name + '正在睡觉!');
    }
}
// 原型方法
Animal.prototype.eat = function(food) {
    console.log(this.name + '正在吃:' + food);
};
console.log('')

/** 1.构造函数模式*/
function Dog(name) {
    Animal.call(this, name)
}

var dog = new Dog('qiyue')
console.log(dog.name)
console.log(dog.share)

//  缺点:Animal里面的sleep方法每创建一次实例就会被创建一次
console.log('-------------------------')
/** 2.原型链继承*/
function Cat(name) {
    this.name = name;
}

Cat.prototype = new Animal(); //  这样的一个弊端造成 Animal 里引用类属性share被共享
Cat.prototype.name = 'cat';

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true

console.log('-------------------------')
/** 3.组合继承,即原型链和构造函数组合*/
Animal.prototype.sayName = function () {
    console.log("I'm a person")
}
function Person(age) {
    Animal.call(this)
    this.age = age
}
Person.prototype = new Animal()
Person.prototype.constructor = Person; //    Person.prototype重新赋值后constructor会丢失

var person = new Person(18)

console.log(person.name)
console.log(person.age)
person.sayName()
//  调用了两次父构造函数
console.log('-------------------------')
/** 4.Object.create的原理继承*/
function createObj(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

var obj = createObj(new Animal())
console.log(obj.name)
console.log('-------------------------')
/** 5.寄生式继承 :
 * 创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象*/
function jisheng (o) {
    var clone = createObj(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}

console.log('-------------------------')
/** 6.寄生组合继承:
 * 只调用了一次 Animal 构造函数,避免了在 Animal.prototype 上面创建不必要的、多余的属性。
 * 与此同时,原型链还能保持不变;
 * 因此,还能够正常使用 instanceof 和 isPrototypeOf。
 * 开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。*/
function Duck(name, age) {
    Animal.call(this, name)
    this.age =  age
}
Duck.prototype = createObj(Animal.prototype)
/**  执行了以下三步
 *  var F = function () {};
 *  F.prototype = Animal.prototype;
 *  Duck.prototype = new F();
 */

var duck = new Duck('duck')
console.log(duck.name)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值