我有这个代码的 poltergeist:
javascript代码:
var cache = {};
cache.init = function()
{
if(typeof(this.memory)=='undefined')
this.memory = {};
};
cache.set = function(key, value)
{
this.memory[key] = value;
};
cache.get = function(key)
{
if(console)
{
console.log("memory: ");
console.log(cache.memory);
console.log("key: "+key);
console.log(cache.memory[key]);
}
if(typeof(cache.memory[key])!='undefined')
return cache.memory[key];
return false;
};
var route = {};
route.load = function(url)
{
var route_list = cache.get('route_list');
if(route_list==false)
{
route_list = this.getList();
cache.set('route_list', route_list);
}
return route_list;
};
route.getList = function()
{
var result = false;
$.ajax(
{
url: 'route.json',
dataType: 'json',
async: false,
success: function(json)
{
result = json;
}
});
return result;
};
cache.init();
route.load();
route.load();
我想用“缓存”设置和获取在客户端缓存一些对象。
route.json 文件的内容:
{
"/example":
{
"controller": "example"
},
"/example/show":
{
"controller": "example",
"method": "show"
}
}
萤火虫的结果是:
memory:
Object
{
route_list
Object { /example={...}, /example/show={...}}
}
key: route_list
undefined
为什么未定义?我可以使用 firebug oO 在对象内部导航
编辑:我添加了第二次调用 route.load(); 我得到未定义的密钥