범위 밖에있는 변수 (함수의 값을 가짐)에 액세스 할 수 있는지 궁금합니다. 다음과 같은 코드가 있습니다.함수 내의 javacript 함수
function parentFunction(){
var childFunction = function() {
// do something
}
}
$(function(){
// need to access childFunction() here.
});
범위 밖에있는 변수 (함수의 값을 가짐)에 액세스 할 수 있는지 궁금합니다. 다음과 같은 코드가 있습니다.함수 내의 javacript 함수
function parentFunction(){
var childFunction = function() {
// do something
}
}
$(function(){
// need to access childFunction() here.
});
var childFunction;
function parentFunction(){
childFunction = function() {
// do something
}
}
$(function(){
childFunction();
});
childFunction이 parentFunction에서 클로저를 캡처하면 유용하다고 생각하지만 그렇지 않으면 네임 스페이스를 잃고 전역 네임 스페이스를 오염시킵니다. – Matt
이것이 전 세계적으로 모두 일어나고 있다고 가정하고, 나는 단순히 범위를 보여주고 있습니다 ... – jondavidjohn
그것은 효과적이었습니다 - 나는 parentFunction을 먼저 호출하여 childFunction의 값을 초기화해야했습니다. 그런 다음 childFunction을 호출 할 수있었습니다. 고맙습니다. – donkapone
아니요.
var parentFunction = (function(){
var actualParentFunction = function(){
this.childFunction = function() {
// do something
};
}
return new actualParentFunction();
})();
하는 당신이 할 수있는 점 :
parentFunction.childFunction();
는'parentFunction()'은'childFunction을 반환 할 수 있습니다이를 달성하는 유일한 방법은 원하는 childFunction parentFunction의 속성을 확인하는 것입니다()'변수가 실행 종료 시점에 있습니까? 아니면'parentFunction()'을 호출하기 전에'childFunction()'에 영향을 주려고합니까? – Rodaine