我正在尝试为对象内部的函数覆盖一个名为“localStorage”的本机方法。
这是我正在尝试做的事情的要点:
function SomeObject(){
this.localStorage = "aaa"; //block access to localStorage for functions INSIDE this object.
... (some more code here)
_testRun(){
window.testA = localStorage; //chose to store the instance on a window (global-like) object
}
this.testRun = function(){ _testRun(); };
this.testRun2 = function(){ window.testB = localStorage;};v
}
var a = new SomeObject();
a.testRun();
a.testRun2();
(after this, when I look up window.testA and window.testB, they both point to the Native localStorage, not the custom one inside the SomeObject.)
顺便说一句,我不想覆盖整个文档的本机函数。(即可能在对象外使用本机 localStorage)
关于如何做到这一点的任何建议/解决方案?谢谢!