我想在HTML5中存储JavaScript对象localStorage,但我的对象显然正在转换为字符串.
我可以使用存储和检索原始JavaScript类型和数组localStorage,但对象似乎不起作用.他们应该吗?
这是我的代码:
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);
Run Code Online (Sandbox Code Playgroud)
控制台输出是
typeof testObject: object
testObject properties:
one: 1 …Run Code Online (Sandbox Code Playgroud) 如何在JavaScript中检查空值?我写了下面的代码,但它没有用.
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在JavaScript程序中找到错误?
我正在使用系统在网站发生重大更新时提醒用户,我使用LocalStorage,当我制作系统时,我让系统检查提示是否为"null",然后将"tip"设置为他们得到警报时是真的.现在,我想将'tip'localstorage设置为null,并改为使用"tip2".
我会这样做吗?localStorage.setItem('tip','null');