我正在写一个safari扩展,当我使用匿名函数回调扩展名get请求时,我失去了调用模块的上下文。
有人知道调用回调时维护模块上下文的方法吗?
var MyModule = (function () {
var _privateVariable = "test";
var doStuff = function () {
SafariExtensionUtilities.Get("https://test.com", function(success, data, response){
//value of private variable no longer set and is undefined
var x = typeof _privateVariable === "undefined"; //true
//value of public variable is set and has correct value
var y = typeof MyModule.PublicVariable === "undefined"; //false
var z = MyModule.PublicVariable === "test"; //true
});
};
return {
PublicVariable: _privateVariable,
};
})();
回叫代码
eval("var fn = " + event.message.eventName);
callback(true, data, response);