자바 스크립트 코드에 문제가 있습니다.콜백 함수의 가변 범위
나는 MyClass
클래스를 가지고 있으며 프로토 타입에 myFunction
함수를 추가했습니다.
MyClass.prototype.myFunction = function(file){
if(some condition){
fs.exists("./" + file, function(exists){
if(exists)
console.log(this.someValue);
/* lot of other code */
else
/* do something else */
});
}else{
/* do something */
}
}
내 문제는 범위 지정 this.someValue
입니다 (예로 인쇄하고 싶습니다). 매번 exists
은 true
이고 콘솔은 undefined
을 기록하지만 그렇지 않습니다. fs.exists()
외부에서 인쇄하는 경우 값이 있으므로 범위 지정 문제라고 생각합니다.
이 샘플에서 this.someValue
에 액세스하려면 어떻게합니까?
미리 감사드립니다.
'{위치 : 런던 '}이 (가) 유효하지 않습니다. fwiw – naomik
오타 .. 감사합니다. – moka