js 의 apply 와 call 방법

1000 단어
js call apply   
        ,apply call           this   .

    1.call()  
      :
        <1> obj.call(thisObj,arg1,arg2,...)
         obj(this)   thisObj,    thisObj   obj      ,   thisObj   obj      .

    2.apply()  :
          :
        <1>obj.apply(thisObj,[arg1,arg2,...])
          obj(this)   thisObj,    thisObj   obj      ,   thisObj   obj      .

      :apply        ,call        .
//1.    :
    var Parent = function () {
        this.name = "mayun";
        this.age = 18;
    }
    var child = {};
    console.log(child) //    : {}
    Parent.call(child)
    console.log(child)//    : {name:"mayun",age:18}

    //2.       :
    var test1 = {
        name:"test1",
        showName: function () {
            console.log(this.name)
        }
    }

    var test2 = {
        name:"test2"
    };
    test1.showName.call(test2) //    :test2
    //  showName  this     test2,this.name  test2.name

좋은 웹페이지 즐겨찾기