0

我正在尝试为对象内部的函数覆盖一个名为“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)

关于如何做到这一点的任何建议/解决方案?谢谢!

4

1 回答 1

0

尝试添加window.localStorageandthis.localStorage而不仅仅是localStorage

function SomeObject(){
   this.localStorage = "aaa"; //block access to localStorage for functions INSIDE this object.
      ... (some more code here)
   _testRun(){
      window.testA = window.localStorage; //chose to store the instance on a window (global-like) object
   }
   this.testRun = function(){ _testRun(); };
   this.testRun2 = function(){ window.testB = this.localStorage;};
}
于 2013-03-02T06:57:58.507 回答