如何在 javascript 中保存对象变量的状态
How do i save state of object vars in javascript?
这是我的代码:
var theObj = {
localepath:false,
settings:false,
localeObj:false,
callAjax: function(relative_path, callback){
var Ajax = new XMLHttpRequest();
var serialized = false;
Ajax.onreadystatechange = function() {
//Since what we are calling a local file. we cannot get a 200 OK Status.
//So We check only the readystate
if(Ajax.readyState==4){
serialized = Ajax.responseText;
callback(serialized);
}
}
Ajax.open("GET",relative_path, true);
Ajax.send();
},
readSettings: function(callback){
var data = this.callAjax('settings.json', function(data){
this.settings = JSON.parse(data);
callback(this.settings);
});
},
readLocale: function(localepath, callback){
var data = this.callAjax('locale/'+localepath+'.json', function(data){
this.localeObj = JSON.parse(data);
callback();
});
},
writeContent: function(id, typeofElement, content){
var currentElement = document.createElement(typeofElement);
currentElement.setAttribute('id', id);
currentElement.innerHTML = this.l10n(content);
document.body.appendChild(currentElement);
},
l10n: function(content){
var localeObject = this.localeObj;
alert(localeObject); //<< This will alert the cause
var arr = content.split(" ").reverse(); //Split by space. and reverse (actually non-reversed) the order
var newArr =[]; //Container
for (var i = arr.length - 1; i >= 0; i--) {
// if the word does not have one-to-one mapping:
if(localeObject[arr[i]] == undefined){
if(isNaN(Number(arr[i]))){
//^^ if the word is not a number
newArr.push(arr[i]); //do not translate.
} else { //if the word is collection of digits
var nums = arr[i].split('').reverse(); //Same old split and (non)-reverse
var convNums = []; //Same old container
for (var j = nums.length - 1; j >= 0; j--) {
convNums.push(localeObject[nums[j]]);
//iterate and push digits
}
newArr.push(convNums.join('')); //Make those digits a word! :)
}
} else { //This means the word has a one-to-one mapping:
newArr.push(localeObject[arr[i]]);
}
}
return newArr.join(" "); //Join all with spaces.
},
setLocalePath: function(path_to_locale){
this.localepath = path_to_locale;
},
getLocaleDefault:function(settings){
return settings.locale[settings.defaultLocale];
}
};
现在,当我这样做时:
theObj.readSettings(function(settings){
theObj.readLocale(theObj.getLocaleDefault(settings), function(){
//Start Writing contents here
theObj.writeContent('top', 'div', 'looma-f5');
theObj.writeContent('content', 'div','test 132');
});
});
它两次发出错误警报,这意味着该方法theObj.readLocale()
没有将内容保存到this.localeObj
?为什么会这样?我尝试做var newObj = new theObj();
但没有奏效。有什么解决方法吗?
这种类型的
问题是由于对this
关键字的理解较少而引起的。这里的主要问题是该方法的localeObj
属性从未保存,因为this
是从回调内部引用的,因此没有引用对象。可以做的一件事是:
.....
readLocale: function(localepath, callback){
var that = this; //Now `that` = `this` references the object
var data = this.callAjax('locale/'+localepath+'.json', function(data){
that.localeObj = JSON.parse(data);
callback();
});
},
....
相关文章:
- 如何在HTML中显示服务器对象变量
- 在Javascript中重新分配对象变量时,原始对象会发生什么
- Javascript 方法看不到对象变量
- JavaScript对象范围-在对象方法中使用对象变量
- 将对象(变量)传递给angularjs指令
- 对象变量在 JavaScript 中是不可访问的
- 无法访问公共JavaScript对象变量
- 可以't获取具有“t”的对象变量;这个“;由setTimeout()函数调用的对象函数中的属性
- Javascript:如何在回调中设置父对象变量
- 从方法内部设置对象变量
- 访问对象中的对象变量
- JavaScript 中的对象变量
- 从 json 代码中删除 javascript 对象变量名称文本
- 将变量传递到对象变量中
- JavaScript 如何重新识别事件对象变量
- 从对象变量中获取子节点
- 使对象变量在所有“更深”的范围内都是可访问的
- 从方法 JavaScript 访问对象变量
- 如何在 javascript 中保存对象变量的状态
- 无法访问IE和Mozilla中javascript的类对象变量