我遇到了一个问题,我的JavaScript代码。回调函数中的变量范围
我有一个类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
?
在此先感谢!
'{location:London''无效,fwiw – naomik
错字..感谢您发现。 – moka