js继承

首先我们来看看call和applay的区别

call方法: 
语法:call(thisObj,Object)
定义:调用一个对象的一个方法,以另一个对象替换当前对象。
说明:
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
apply方法: 
语法:apply(thisObj,[argArray])
定义:应用某一对象的一个方法,用另一个对象替换当前对象。 
说明: 
如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。 
如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。


1.采用call方法:改变函数内部的函数上下文this,使它指向传入函数的具体对象。但是缺点是该种方式不能继承原型链。

//通过call方法
    function fuck(godie) {
        this.name="fuck you";
        this.string="gun";
        this.do=godie;
        this.mood=function () {
           console.log(this.name);
        }
    }
    function gun(godie) {
        this.want=function () {
            console.log(this.name);
        }
        this.doing=function () {
            console.log(this.string);
        }
       this.youneed=function () {
    return godie;
}
    }
    var Fuck=new gun("axiba");
  fuck.call(Fuck,Fuck.youneed());
    console.log(Fuck.do);
    Fuck.mood();

2.采用apply方法与上面的区别在于上面call传入的参数不能是数组,而apply需要传入数组

//通过applay方法
    function fuck(godie) {
        this.name="fuck you";
        this.string="gun";
        this.do=godie;
        this.mood=function () {
            console.log(this.name);
        }
    }
    function gun(godie) {
        this.want=function () {
            console.log(this.name);
        }
        this.doing=function () {
            console.log(this.string);
        }
        this.youneed=function () {
            return godie;
        }
    }
    var Fuck=new gun("axiba");
    fuck.apply(Fuck,[Fuck.youneed()]);
    console.log(Fuck.do);
    Fuck.mood();

3.采用原型链的方式实现继承     实现原理:使子类原型对象指向父类的实例以实现继承,即重写类的原型,弊端是不能直接实现多继承
    function Parent()  
    {  
      
        this.sayAge=function()  
        {  
            console.log(this.age);  
        }  
    }  
    function Child(firstname)  
    {  
        this.fname=firstname;  
        this.age=40;  
        this.saySomeThing=function()  
        {  
            console.log(this.fname);  
            this.sayAge();  
        }  
    }  
      
    Child.prototype=new  Parent();  
    var child=new Child("张");  
    child.saySomeThing();  
4.使用对象冒充实现继承(该种实现方式可以实现多继承)
实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值

    function Parent(firstname)  
    {  
        this.fname=firstname;  
        this.age=40;  
        this.sayAge=function()  
        {  
            console.log(this.age);  
        }  
    }  
    function Child(firstname)  
    {  
        this.parent=Parent;  
        this.parent(firstname);  
        delete this.parent;  
        this.saySomeThing=function()  
        {  
            console.log(this.fname);  
            this.sayAge();  
        }  
    }  
    var mychild=new  Child("李");  
    mychild.saySomeThing();  

5.采用混合模式开发
    function Parent()  
    {  
      
        this.sayAge=function()  
        {  
            console.log(this.age);  
        }  
    }  
      
    Parent.prototype.sayParent=function()  
    {  
       alert("this is parentmethod!!!");  
    }  
      
    function Child(firstname)  
    {  
        Parent.call(this);  
        this.fname=firstname;  
        this.age=40;  
        this.saySomeThing=function()  
        {  
            console.log(this.fname);  
            this.sayAge();  
        }  
    }  
      
    Child.prototype=new  Parent();  
    var child=new Child("张");  
    child.saySomeThing();  
    child.sayParent();  





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值