이 내가 여기자바 스크립트 클로저 문제가 다시 발생합니다.
function AAA(){ this.f1 = function(){ /*expecting that f2 will be call as both of then are of same object.*/ console.log(f3(5));// } /*naming the function f3 hope to get function reference, as function on the right side has name now and in the closure of f1, right?*/ this.f2 = function f3(x){ return x; } function f4() {}; } x = new AAA(); x.f1(); //ReferenceError: f3 is not defined
을 무슨 일이 벌어지고,
function AAA(){ this.f1 = function(){ /*expecting that f2 will be call as both of then are of same object.*/ console.log(f2(5));// } this.f2 = function(x){ return x; } } x = new AAA(); x.f1(); //ReferenceError: f2 is not defined
어느 작동합니까에 직면하고 무엇인가? 누가 'f4'를 제외하고 'f1'을 닫았습니까? 'this'없이는 같은 객체 함수를 호출 할 수 없습니까?
: this.f2'라는 이름의 변수를 생성하지 않습니다'f2''에 할당하지만, 코드 당신이 당신의 코드가 작동하려면, 당신은 단지 이것에 대한 참조가 필요 'f2'라는 변수 (!)에 접근하려고 시도합니다. –
@Chistopher 함수 이름을 f3으로 할 때 두 번째 예제가 어떻게됩니까? – Zer001