2016-11-30 1 views
3

자바 스크립트에서 Function.call()this 값과 0 개 이상의 인수가 주어진 경우 Function을 호출 할 수 있습니다.왜 내가 .call() 함수 호출을 할 수 없습니까?

Function.call 자체가 기능입니다. 따라서 이론적으로 Function.callFunction.call.call과 같은 (또는 유사하게 작동하는) 함수 여야합니다. 내가 Function.call.call()를 호출 할 수 없습니다,

> Function.call() 
[Function: anonymous] 

그러나

> Function.call === Function.call.call 
true 

우리가 Function.call() 전화

, 우리는 익명 함수를 얻을 : V8에서

,이 경우 것 같다. 여기

> Function.call.call() 
TypeError: undefined is not a function 
at repl:1:21 
at REPLServer.defaultEval (repl.js:132:27) 
at bound (domain.js:291:14) 
at REPLServer.runBound [as eval] (domain.js:304:12) 
at REPLServer.<anonymous> (repl.js:279:12) 
at REPLServer.emit (events.js:107:17) 
at REPLServer.Interface._onLine (readline.js:214:10) 
at REPLServer.Interface._line (readline.js:553:8) 
at REPLServer.Interface._ttyWrite (readline.js:830:14) 
at ReadStream.onkeypress (readline.js:109:10) 

을 무슨 일이야? Function.call은 분명히 기능입니다.이 오류 메시지에 나와있는 것처럼 undefined이 아닙니다.

+1

일반적인 프로그래밍 컨텍스트에서 무의미하지만 좋은 질문과 대답 :) – nicovank

+0

@nicovank 물론, 나는 자바 스크립트의 "what if 's"를 탐색하고 있었고 실수로이 문제가 발생했습니다. :) – Qix

+0

[물론 할 수있어] (http://stackoverflow.com/q/31074664/1048572)! – Bergi

답변

4

짧은 대답 : 오류 메시지는 입니다. 매우입니다. 두 번째 .call()

Function.callthis 호출되고있다 : 당신이

(undefined)(); 

긴 대답을 할 때 당신이 얻을 같은 오류 메시지입니다.

매개 변수없이이 매개 변수를 호출하면 이있는 thisthis 값으로 호출됩니다.

따라서, 당신은 정말 당신이 (은유)하고있는 것을 의미한다

Function.call.call(undefined) 

을하고있어 정말

undefined() 

전달 아무것도

undefined.call() 

(또는 undefined)에서 Function.call.call()까지의 this 매개 변수는 필수적입니다 첫 번째 Function.call() (Function 자체)의 this 컨텍스트를 무효화하여 undefined에서 .call()을 호출해야합니다.

이렇게하면 생성되는 오류 메시지가 나타납니다 : undefined is not a function.

+1

... 작동 예제는'Function.call.call (Function)'또는'Function.call.call (console.log, console, "hello")' – Bergi

+0

@Bergi Correct입니다. 다시, 혼란은 _error 메시지 _에서 비롯되었습니다. – Qix

+2

아, 나는 당신이 당신의 질문에 스스로 답했다는 것을 깨닫지 못했습니다. 방금 두 번 "콜"사용에 대한 예제를 추가하면 설명에 도움이 될 것이라고 생각했습니다. – Bergi

관련 문제