有没有办法在javascript中重置/清除浏览器的localStorage?
如果我不需要localStorage,我的代码将如下所示:
var names=new Array();
names[0]=prompt("New member name?");
Run Code Online (Sandbox Code Playgroud)
这有效.但是,我需要将这个变量存储在localStorage中,并且证明它非常顽固.我试过了:
var localStorage[names] = new Array();
localStorage.names[0] = prompt("New member name?");
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
localStorage,sessionStorage,session和cookies的技术优势和缺点是什么?我何时使用其中一个?
如何检查项目是否已设置localStorage?目前我正在使用
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
Run Code Online (Sandbox Code Playgroud) 所以,我有一个选项页面,用户可以在其中定义某些选项并将其保存在localStorage中: options.html
现在,我还有一个内容脚本需要获取options.html页面中定义的选项,但是当我尝试从内容脚本访问localStorage时,它不会从选项页面返回值.
如何让我的内容脚本从localStorage,选项页面甚至后台页面获取值?
每个JS意见领袖都说扩展原生对象是一种不好的做法.但为什么?我们是否获得了性能?他们是否害怕有人以"错误的方式"做到这一点,并添加了可枚举的类型Object,几乎破坏了任何对象上的所有循环?
以TJ Holowaychuk的should.js为例.他增加了一个简单的getter来Object,一切工作正常(来源).
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(Object(this).valueOf());
},
configurable: true
});
Run Code Online (Sandbox Code Playgroud)
这真的很有道理.例如,可以扩展Array.
Array.defineProperty(Array.prototype, "remove", {
set: function(){},
get: function(){
return removeArrayElement.bind(this);
}
});
var arr = [0, 1, 2, 3, 4];
arr.remove(3);
Run Code Online (Sandbox Code Playgroud)
是否有任何反对扩展本机类型的论据?
是否可以永久更改javascript变量?如同,如果我设置变量X并使其等于1.然后单击按钮将该变量更改为2.如何在刷新页面时将该变量保持为2?
我注意到我无法设置布尔值localStorage?
localStorage.setItem("item1", true);
alert(localStorage.getItem("item1") + " | " + (localStorage.getItem("item1") == true));
Run Code Online (Sandbox Code Playgroud)
总是true | false在我尝试测试警报时localStorage.getItem("item1") == "true"发出警报...所以我无法将项目设置localStorage为true?
即使它是一个字符串,我认为只会===检查类型?
所以
alert("true" == true); // should be true?
Run Code Online (Sandbox Code Playgroud) 我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性.
它还具有我不想传递给服务器的功能.我如何序列化和反序列化这些对象?
我读过这样做的最好方法是使用Douglas Crockford的stringify.但是,我在Chrome中收到以下错误:
TypeError:将循环结构转换为JSON
代码:
function finger(xid, xparent){
this.id = xid;
this.xparent;
//other attributes
}
function arm(xid, xparent){
this.id = xid;
this.parent = xparent;
this.fingers = [];
//other attributes
this.moveArm = function() {
//moveArm function details - not included in this testcase
alert("moveArm Executed");
}
}
function person(xid, xparent, xname){
this.id = xid;
this.parent = xparent;
this.name = xname
this.arms = []
this.createArms = function () {
this.arms[this.arms.length] = new arm(this.id, this);
}
}
function group(xid, xparent){
this.id = …Run Code Online (Sandbox Code Playgroud) 使用Javascript在localStorage中查找值的速度有多快?
有没有人有任何性能测试的链接,表明是否值得缓存JavaScript对象中的数据?或者浏览器是否已经缓存了从localStorage访问的值?
我对localStorage的Firefox和Chrome实现特别感兴趣.