상세 컨텐츠

본문 제목

Function methods (call, apply, bind) - Javascript

Programming/Concept

by 쌩우 2019. 4. 16. 15:33

본문

특정 함수에 특정 arguments를 도입하고 싶을 때 사용한다

 

1. call

: Function.prototype.call(thisArg, arg1, arg2, ...)

  
function makeParamsToArray() {
  return Array.prototype.slice.call(arguments);
}

console.log(makeParamsToArray('first', 'second'))

//["first", "second"]

2. apply

: Function.prototype.apply(thisArg, [argsArray])

  
var min = Math.min(7, 35, 2, 8, 21);
console.log(min); //2

var arr = [7, 35, 2, 8, 21];
var min2 = Math.min.apply(null, arr);
console.log(min2); //2

3. bind

  • 인자로 넘겨준 객체와 연결(bind)된 새로운 함수를 반환

  • callback 함수를 특정 객체와 연결하고 싶을 때 사용

        
    //fn.bind(thisArg\[, arg1\[, arg2\[, ...\]\]\])
    
    function foo(){
    return this;
    }
    
    var boundFoo = foo.bind({a:1});
    foo(); //Window
    boundFoo(); // {a:1}
    

'Programming > Concept' 카테고리의 다른 글

Asynchronous Call - Javascript  (0) 2019.04.16
Prototype 프로토타입 - Javascript  (0) 2019.04.16
this의 적용  (0) 2019.04.15
Closure Module Pattern  (0) 2019.04.15
Array Methods of Javascript  (0) 2019.04.15

관련글 더보기

댓글 영역