2013-03-02 89 views
0

我試圖覆蓋一個名爲「localStorage」的本地方法,用於INSIDE對象中的函數。覆蓋原生方法和對象INSIDE特定對象

這裏是我想要做一個要點:

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.) 

BTW,我不想覆蓋原有的功能,整個文檔。 (即可能使用本地localStorage的對象外)

關於如何做到這一點的任何建議/解決方案?謝謝!

+0

嘗試window.testB = this.localStorage,它應該涉及到範圍變量。 – Derek 2013-03-02 06:56:08

回答

0

嘗試添加window.localStoragethis.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;}; 
} 
+0

對不起Derek,但這不是我正在尋找的......我有點試圖「隱藏」函數的全局變量INSIDE特定對象 – user1894397 2013-03-03 13:11:14