2012-09-17 4 views
1

var that = this으로 해결하지 않고 this을 즉시 호출 함수 식에 전달할 수 있습니까? (경우에 따라 다름)?즉시 호출 함수 표현식에이 함수를 전달

(function(that) { 
    console.log(that); 
})(this) 
+0

문맥 상 이것은 무엇입니까? 그 함수를 객체 또는 함수 컨텍스트에서 호출하고 있습니까? .. function.apply (context, arguments) 폼이 당신이 찾고있는 것일 수 있습니다 .. –

답변

5

당신은이 목적을 위해 call 또는 apply을 사용할 수 있습니다

는 다음하지만 운 시도. 예를 들어 :

(function() { 
    console.log(this); // whatever that was specified in the "call" method 
}).call(this); 
0
(function(that) { 
    console.log(that); 
})(this); 

코드는 작동합니다, 앞에 아무 코드가 세미콜론없이이 없습니다 있는지 확인하십시오.

(function(that) { 
     console.log(that); 
})(this) // if here is no semicolon, the next code will be syntax error. 
(function(that) { 
     console.log(that); 
})(this); 

세미콜론을 생략하기 전에 아래 코드를 사용해도 괜찮습니다.

!function(that) { 
    console.log(that); 
}(this); 
관련 문제