프로그래밍/자바스크립트

자바스크립트 - apply와 call 차이점

가카리 2015. 2. 3. 20:14
반응형

함수를 부를 때 apply 와 call 의 차이가 무엇인가요?

var func = function(){
  alert('hello!');
};

func.apply();

vs

func.call();

성능차이가 있는 건가요? 언제 apply() 를 쓰고 언제 call() 을 쓰나요?

편집게시물 신고삭제publish링크

게시 Jul 19 '13

director의 그라바타 이미지

번역 감사합니다:)

jihae (Jul 23 '13)
0

apply 는 함수 인자를 배열 형태로 전달하고, call 은 인자를 일반적인 방법, 즉 쉼표로 구분지어서 전달합니다.

여기와 여기 를 참조하세요.

사용 패턴은 다음과 같고요:

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, ...)

예제 코드입니다:

function theFunction(name, profession) {
    alert("My name is " + name + " and I am a " + profession + ".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");

apply 와 call 의 첫번째 인자는 함수에서 this 가 됩니다.


출처 : http://codeflow.co.kr/question/216/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C-call-%EA%B3%BC-apply-%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90%EC%9D%B4-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80%EC%9A%94/


반응형