JQuery Extend - Firefox 不支持操作
使用 JQuery 时,扩展具有重写的 toString() 函数的对象会导致 Firefox 中出现“不支持操作”错误。然而在 Chrome 中它工作得很好。这是 JQuery 中的错误还是我在下面的代码片段中做错了什么?
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
When using JQuery, extending an object that has an overridden toString() function causes an "Operation is not supported" error in Firefox. However in Chrome it works fine. Is this a bug in JQuery or am I doing something wrong in the below code snippet?
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 version 1.4.2
Many thanks,
Godders
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用匿名函数来获取“foo”的值时,
this
变量将引用 window 对象。您为“foo2”调用的匿名函数也是如此。因此,您正在尝试扩展窗口对象。这真的是你想做的吗?编辑 Firefox 似乎遇到的问题是尝试复制
window
的“sessionStorage”属性。尝试添加这一行:你会得到完全相同的错误。
When you call the anonymous function to get the value for "foo", the
this
variable will be referencing the window object. Same goes for the anonymous function you call for "foo2". Thus, you're trying to extend the window object. Is that really what you want to do?edit what Firefox seems to be tripping over is the attempt to copy the "sessionStorage" attribute of
window
. Try adding this line:and you'll get the exact same error.