我有一个javascript函数:
function Letter(){
if(typeof Letter.instance === 'object'){
return Letter.instance;
}
Letter.instance = this;
var self = this;
this.getList = function(id, key, startdate, enddate){
console.log(Letter.qList);
var url = getURL() +'?id=' + id + '&key='+ key + '&start=' + +'&end=' + enddate;
Letter.qList = ajaxRequest(url,"GET","json","application/json",0);
console.log(Letter.qList);
};
}
Letter.qList = null;
qList
是Letter
的全局变量。
这是骨干观点:
var letterHistory = new Letter();
var CustomerView = Backbone.View.extend({
initialize: function() {
},
events : {
'click #search' : 'searchLetterHistory'
},
'searchLetterHistory' : function(){
letterHistory.getList('106','891177-0002','2012-08-07T13:58:19.403','2013-08-17T14:47:35.15');
},
render: function(){
letterHistory.getList('106','','2012-08-07T13:58:19.403','2013-08-17T14:47:35.15');
this.$el.append("<button id='search'>Search history</button>");
}
});
return CustomerView;
当我转到render()
#customer
的结果
点击按钮后:
问题:当我转到#home
路由器并返回#customer
时,我想结果将显示为第一张图片(qList
设置为null)但它没有不像我想的那样,qList
仍然保留着它的价值。
那么每当我路由到另一台路由器时,我怎么能清除全局变量(qList)。
答案 0 :(得分:0)
设置Letter.qList = null; qList是Letter
的“静态”变量 if(typeof Letter.instance === 'object'){
Letter.qList = null;
return Letter.instance;
}
或者你可以调用Letter.qList = null;你代码中的每一个地方
答案 1 :(得分:0)
尝试在返回现有对象时删除qList
属性:
function Letter(){
if(typeof Letter.instance === 'object'){
delete Letter.qList; // *** notice this line ***
return Letter.instance;
}
Letter.instance = this;
var self = this;
this.getList = function(id, key, startdate, enddate){
console.log(Letter.qList);
var url = getURL() +'?id=' + id + '&key='+ key + '&start=' + +'&end=' + enddate;
Letter.qList = ajaxRequest(url,"GET","json","application/json",0);
console.log(Letter.qList);
};
}