자바스크립트 함수 호출 실행 ,  call apply bind
https://www.zerocho.com/category/JavaScript/post/57433645a48729787807c3fd

call()과 apply()는 함수를 실행하는 기능이 같음
다른점 : 2번째 인자 받는 형식
call() :  개별적으로 나열 , function1.call(this, arg1, arg2 )
apply() : 배열 , function1.apply(this, [arg1, arg2] )


* call(), apply()의 기능
1번째 인자가 this를 대체
다른 객체의 속성을 변경시킬수 있음

    var obj = {
      string: 'zero',
      yell: function( arg1 ) {
        console.log(this.string, arg1, this);
      }
    };
    var obj2 = {
      string: 'what?'
    };
    obj.yell(); // 'zero';
    obj.yell.call(obj2, arg1); // 'what?' , obj2가 obj의 this를 대체


bind : call()과 용법은 같고, this를 변경만 하고, 함수실행은 하지 않음
var yell2 = obj.yell.bind(obj2);//
yell2(); // 실행은 별도로

 

//---------------------

// 참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/call

 

 

반응형
Posted by codens