我正在尝试将字段的值设置为函数,然后执行它。this.fetchLocalStorage is not a function
是我从运行它中得到的。
var app = {
busdata: (function(){return this.fetchLocalStorage()})(),
fetchLocalStorage: function() {
//fetching
return "fetching data...";
}
};
console.log(app.busdata);
请注意,通过不使其成为自执行函数,它可以工作,但这意味着每次我只需要一次获取数据时都会调用该函数。
busdata: function(){return this.fetchLocalStorage()}
/* ... */
console.log(app.busdata()); //this calls the function every time :(
认为这可能是一个上下文问题,所以我尝试了一些事情,bind
但call
没有运气。我错过了什么吗?