【问题标题】:Mootools extends the "Function" class with an "extend" method making jQuery unusableMootools 使用“extend”方法扩展“Function”类,使 jQuery 无法使用
【发布时间】:2011-05-07 01:18:15
【问题描述】:

Mootools 扩展了“Function”类并在其中添加了一个名为“extend”的新方法。现在 jQuery 尝试使用 jQuery.prototype.extend 添加“扩展”功能。然而,由于“扩展”已经是 jQuery 对象的一部分(因为 jQuery 是 Function 类的对象),所以 jQuery.prototype.extend 不起作用。 有人在同时使用 Mootools 和 jQuery 时遇到过这种冲突吗?

更一般地说,如果像“函数或数组或对象”这样的原生类被扩展,我们是否有办法恢复到原始定义?

【问题讨论】:

  • 如果您在本地定义原型链,为什么还要关心它 - 如果它在该范围内看到 jQuery.prototype.extend,它将/不应该转到 Function.prototype.extend... @ 987654321@他们似乎没有检查原型链的功能
  • github.com/jquery/jquery/blob/master/src/core.js#L68jQuery.fn 实际上定义为等于 jQuery.prototype

标签: javascript jquery oop mootools mozilla


【解决方案1】:

你试过 jQery.noConflict 吗? http://api.jquery.com/jQuery.noConflict/

【讨论】:

  • 是的,我有。 noConflict 有助于避免与 "$" 的冲突。然而,这里不是关于变量命名。它是关于扩展基类的。
【解决方案2】:

我能想到的唯一方法:

<script type="text/javascript">
    // copy the original function
    var ext = Function.prototype.extend;
    // remove it
    delete Function.prototype.extend;
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 

<script type="text/javascript">
    // Copy the jQuery version
    var jqext = jQuery.prototype.extend;
    // remove it (for sanity).
    delete jQuery.prototype.extend;

    // reassign the original function.
    Function.prototype.extend = ext;
    // remove the jQuery extend method (now the original Function.extend method)
    delete jQuery.prototype.extend;
    // reassign jQuery's original extend method.
    jQuery.prototype.extend = jqext;
</script>

【讨论】:

  • 该解决方案运行良好。但是我不明白为什么我们要删除 jQuery.prototype.extend(出于理智考虑),然后在重新评估 function.prototype.extend 后再次删除。改变基类是否也会改变已经继承的类?
  • 我删除了它,因为我不喜欢需要补偿如果Function.prototype.extend = ext; 弄乱它可能会发生的事情的想法。我发现分配给空变量名的可能性较小,需要调试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2018-07-14
  • 2016-08-20
相关资源
最近更新 更多