【发布时间】:2012-04-25 11:50:17
【问题描述】:
我们有一个广泛使用 jQuery 的网站,它在 Firefox 和 IE 中运行良好。但是在 Chrome 中,我们经常(和半随机地)获得Uncaught TypeError: Cannot call method 'apply' of undefined(也有其他 jQuery 方法出现在 apply 的位置)。
我们设法将问题追溯到 jQuery 方法pushStack()。
原始源代码(jQuery 1.7.1):
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = this.constructor();
// (etc.)
}
仪表代码:
pushStack: function( elems, name, selector ) {
if (!(this instanceof jQuery.fn.init)) throw this;
// Build a new jQuery matched element set
var ret = this.constructor();
if (!(ret instanceof jQuery.fn.init)) {
console.log("pushStack>this: " + this.constructor);
console.log("pushStack>ret: " + ret.constructor);
throw ret;
}
// (etc.)
}
在大多数情况下pushStack() 运行正确。然而,有时 Chrome 会构造一个 Object 类型的对象,而不是 jQuery.fn.init。控制台输出:
pushStack>this: function ( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
pushStack>ret: function Object() { [native code] }
Uncaught #<Object>
有人遇到过类似的问题吗?它是 Chrome 的(已知)错误吗?
更新
我设法简化了我们的页面,以便它可以自行加载。我填写了bug in Chromium project 项目,复制问题的页面附在此处。
【问题讨论】:
-
你能缩小用例的范围吗?这听起来像是常见的
console.log.apply上下文问题。 -
@David 感谢您的评论。
console.log.apply上下文问题是什么意思?使用没有检测的原始 jQuery 时也会出现此问题。 -
@MiroslavBajtoš 您是否应用了 Chromium 项目中建议的解决方法?
-
@davidzarlengo 是的,我是该解决方法的作者。
-
哦,好吧,在那种情况下,谢谢!到目前为止,它也对我们有用。
标签: javascript jquery google-chrome