简体   繁体   English

有没有办法包装jQuery的isArrayLike?

[英]Is there a way to wrap jQuery's isArrayLike?

I'm trying to handle a conflict between XenForo and an essential plugin. 我正在尝试处理XenForo和基本插件之间的冲突。 Neither is code I can directly modify. 我也无法直接修改代码。 jQuery 1.11.3 is the version in use, and that can't be upgraded either. jQuery 1.11.3是正在使用的版本,也无法升级。

I can wrap the calling function with a try/catch and avoid the conflict, but the real fix would involve wrapping jQuery's isArrayLike with a try/catch. 我可以用try / catch包装调用函数并避免冲突,但是真正的解决方法是用try / catch包装jQuery的isArrayLike。

isArrayLike is in the (current version) jQuery source at line 464 in https://github.com/jquery/jquery/blob/master/src/core.js#L464 isArrayLike在https://github.com/jquery/jquery/blob/master/src/core.js#L464第464行的(当前版本)jQuery源中

What I need to catch seems to be exactly what was being debated in https://forum.jquery.com/topic/jquery-isarraylike-for-consistency with regard to isArrayLike throwing errors when passed invalid types. 我需要了解的似乎正是https://forum.jquery.com/topic/jquery-isarraylike-for-consistency中关于isArrayLike在传递无效类型时引发错误的辩论。

So, since jQuery's isArrayLike isn't exposed, is there any way to either wrap it, or worst case, get in there and replace it? 因此,由于未公开jQuery的isArrayLike,是否有任何方法可以包装它,或者在最坏的情况下进入并替换它?

Edit: Note, all the comments so far are trying to debug the error itself. 编辑:注意,到目前为止,所有注释都在尝试调试错误本身。 That is not my question. 那不是我的问题。 I am asking if there is any way to access isArrayLike, so it can be wrapped with another function. 我问是否有任何方法可以访问isArrayLike,因此可以将其与另一个函数包装在一起。

I would suggest replacing the jQuery each and map methods, which seem to be the only methods calling on isArrayLike , except for makeArray . 我建议替换jQuery eachmap方法,这似乎是调用isArrayLike的唯一方法,但makeArray除外。 But the fix I am suggesting is not necessary for the latter method. 但是我建议的修复对于后一种方法不是必需的。

Add this: 添加:

(function ($, origEach, origMap) {
    $.each = function (elems, callback, arg) {
        return origEach.call(this, Object(elems), callback, arg);
    };
    $.map = function (elems, callback, arg) {
        return origMap.call(this, Object(elems), callback, arg);
    };
})(jQuery, jQuery.each, jQuery.map);

It alters the first argument that is passed to map or each : it gets wrapped in an Object call, which changes nothing when it is an array, but turns the argument in an object if it is not an object (for some odd reason). 它更改传递给mapeach的第一个参数:它包装在Object调用中,当它是数组时,它不会改变任何内容,但是如果它不是对象,则在对象中转换参数(出于某种奇怪的原因)。

Patching jQuery 修补jQuery

If you can use a patched version of jQuery, just store a copy of jQuery, and modify this line in the function isArrayLike : 如果可以使用修补版本的jQuery,只需存储jQuery的副本,然后在isArrayLike函数中修改此行:

var length = !!obj && "length" in obj && obj.length,

to: 至:

var length = !!obj && "length" in Object(obj) && obj.length,

It should not be a problem for XenForo , as long as you explicitly add the patched jQuery via another script tag, after having included XenForo . 对于XenForo ,这应该不是问题,只要包含XenForo 之后通过另一个script标签显式添加修补的jQuery。 The latest jQuery object overwrites the previous one, so XenForo will then also use the patched version, even though it included the non-patched jQuery library itself. 最新的jQuery对象将覆盖前一个对象,因此XenForo也将使用已修补的版本,即使它包含未修补的jQuery库本身也是如此。

The downside of patching is that you cannot upgrade jQuery (implicitly with a XenForo upgrade) unless you patch it every time you upgrade (until the version where it is no longer necessary). 修补程序的缺点是您不能升级jQuery(暗含XenForo升级),除非您在每次升级时都对其进行修补(直到不再需要的版本)。

That is a problem you won't have with the first solution. 这是第一个解决方案不会遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM