2010-08-23 56 views
1

使用JQuery時,擴展具有重寫的toString()函數的對象會導致Firefox中出現「Operation is not supported」錯誤。但在Chrome中,它工作正常。這是JQuery中的錯誤還是我在下面的代碼片段中做錯了?JQuery Extend - 在Firefox中不支持操作

var foo = function() { 
     var that = this; 

     that.toString = function() { return "foobar" }; 

     return that; 
    }(); 

    var foo2 = function() { 
     var that = this;    

     that = $.extend(true, {}, foo); // copy = options[ name ]; = "Operation is not supported" in Firefox 3.6.8 

     return that; 
    }(); 

    alert(foo.toString()); //"foobar" in Chrome 
    alert(foo2.toString()); //"foobar" in Chrome 

JQuery的版本1.4.2

非常感謝,

Godders

回答

1

當調用匿名函數來獲取 「foo」 的值,則this變量會引用窗口對象。您爲「foo2」調用的匿名函數也一樣。因此,您試圖擴展窗口對象。那真的是你想要做的嗎?

編輯 Firefox似乎在跳過的是嘗試複製window的「sessionStorage」屬性。嘗試添加此行:

var test = window['sessionStorage']; 

並且您將得到完全相同的錯誤。

+0

感謝您的回答。這促使我回去重讀克羅克福德的「優秀部分」一書中的4個調用模式。 – Godders 2010-08-23 15:12:37

+0

好吧,所以我想要做的事情需要var that = {};在foo和foo2函數中。謝謝您的幫助。 – Godders 2010-08-23 15:23:30

+0

不客氣!祝你好運! – Pointy 2010-08-23 16:42:28