美文网首页
apply和call

apply和call

作者: 可爱的木头 | 来源:发表于2017-05-03 17:22 被阅读0次

    apply的参数是array call没有限制

    obj.call(thisObj,arg1,arg2,...);
    obj.apply(thisObj,[arg1,arg2,...]);
    

    两者作用一致,都是把obj(即this)绑定到thisObj,这时候thisObj具备了obj的属性和方法。或者说thisObj继承了obj的属性和方法

    var Parent = function(){
        this.name = "yjc";
        this.age = 22;
    }
    
    var child = {};
    
    console.log(child);//Object {} ,空对象
    
    Parent.call(child);
    
    console.log(child); //Object {name: "yjc", age: 22}
    

    相关文章

      网友评论

          本文标题:apply和call

          本文链接:https://www.haomeiwen.com/subject/fbcltxtx.html