对象继承方法

对象继承: 实例化对象给可以继承object(对象)的内容  不能继承函数的内容

原型继承:子类继承父类,让子类的原型指向父类

        子类.prototype =new 父类()

function Animal(name,weight){

        this.name = name;

        this.weight = weight

    }

    Animal.prototype.eat = function(){

        console.log('吃骨头');

    }

    function Dog(color){

        this.color = color

    }

    Dog.prototype = new Animal('白小黑','18')

    Dog.prototype.bitePerson = function(){

        console.log('咬人');

    }

function ErHa(sex){

        this.sex = sex

    }

    ErHa.prototype = new Dog('黑色')

    ErHa.prototype.playHost = function(){

        console.log('玩');

    }

拷贝继承:指深拷贝,深拷贝:拷贝值    浅拷贝:拷贝地址

 for (x in p1) {

    // 只需要拷贝属性值即可

    stu1[x] = p1[x];

  }

 

var animal = new Animal('大象','18')

    var tiger = new Tiger('母老虎')

    for(key in animal){

        tiger[key] = animal[key]

    }

    tiger.eat()

    console.log(tiger);

构造函数继承: call() 方法改变this指向,将父类.call(this,属性)  写到子类的构造函数中使this指向子类构造函数的实例化对象

function Animal(name, weight, age, sex) {

        this.name = name;

        this.weight = weight;

        this.age = age;

        this.sex = sex;

        this.eat = function () {

            console.log('吃骨头');

        }

    }

    Animal.prototype.play = function () {

        console.log('玩耍');

    }

    function Cat(color, name, weight, age, sex) {

        this.color = color

        Animal.call(this, name, weight, age, sex);

    }

    var cat = new Cat('白色', '小白', '18公斤', 18, '公猫')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值