继承

1.对象冒充

说明:让父类的构造函数成为子类的,如:

    function Par(a,b){
        this.name = a;
        this.age = b;
        this.sayName = function(){
            console.log(this.name)
        }
    }
    function Stu(a,b){
        this.obj = Par;
        this.obj(a,b);
        delete this.obj    //删除
    }

2.原型链

访问一个对象时,先在基本属性中查找,如果没有,就沿着_proto_往上查找,这就是原型链。

不能传参数,每一个对象共享数据。

    function Far(){
        this.age = 14;
    }
    Far.prototype.name = "abc";
    function Chi(){

    }
    Chi.prototype = new Far();  //原型链
    var p1 = new Chi();
    console.log(p1.name);  //输出 abc 
    console.log(p1.age);  //输出 14

3.call 和 apply

    function Par(a,b){
        this.name = a;
        this.age = b;
        this.sayName = function(){
            console.log(this.name)
        }
    }
    function Stu(a,b){
            //Par.call(this,a,b) //call的方法
        Par.apply(this,[a,b])   //apply的方法
    }

4.混合模式

    function Gra(a){
        this.name = a
    }
    Gra.prototype.sayName  = function (){
        console.log("aaaa")
    };
    function Fa(b){
        this.age = b;
    }
    Fa.prototype = new Gra(); //原型链继承Gra.prototype的 sayName函数
    function Chi(a,b,c){
        Gra.call(this,a);  //通过call继承Gra函数的name属性
        Fa.apply(this,[b]);  //通过call继承Fa函数的age属性
        this.sex = c;
    }
    Chi.prototype = new Fa(); //原型链继承Fa.prototype的 sayName函数
    var p1 = new Chi("爷爷",88,"男");
    console.log(p1.name);   //输出 爷爷
    console.log(p1.age);    //输出 88
    console.log(p1.sex);    //输出 男
    p1.sayName()  //输出 aaaa
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值