从另一个文件覆盖 JS 函数

2023-11-21

我试图重写 Bigcartel 的 JS 函数。我无法访问 JS 文件。

原文是:

updateCart: function(cart) {
    $('aside .cart .count, .main header .cart').htmlHighlight(cart.item_count);
    return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
  }, 

我正在尝试将其更改为:

updateCart: function(cart) {
  $('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
  return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},

我知道其他人也问过类似的问题,但在理解如何实现 JS 方面我完全是个菜鸟(我只知道如何通过反复试验来调整)

如果有人能好心地帮助我的话给我答案那太好了。

Thanks,

iWed-


编辑 [2013 年 10 月 10 日 :: 21:24 小时]

澄清一下,我无法直接访问原始 JS 文件。我只能通过chrome查看。我只能访问 html 文件。它适用于大卡特尔主题编辑。

这是使用 chrome 复制 JS 的链接。 第 216 行是代码,如果这有帮助的话:http://jsfiddle.net/w9GTJ/


EDIT:你很幸运。从发布的代码中您可以看到 updateCart 方法是在 window.Store 全局对象上导出的。解决方案是在加载原始脚本后添加以下代码:

window.Store.updateCart = function(cart) {
  $('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
  return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
};

一般情况说明:

网页中加载的所有脚本都在相同的全局范围内运行,因此覆盖变量就像随后插入脚本一样简单:

<script>
var x = 5; // original script
</script>
<script>
x = 2; // your inserted script
</script>

从表面上看,您的函数被定义为对象的属性:

var x = {
   updateCart : function(cart) {
     // stuff
   }
}

因此,要覆盖它,您需要执行以下操作:

x.updateCart = function(cart) {
  // your code
}

最后,在一种情况下,如果函数在原始代码中是私有的,则您根本无法覆盖它:

function() {
   var x = {
      updateCart: function(){}
   }
}()

// No way to access x.updateCart here
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从另一个文件覆盖 JS 函数 的相关文章

随机推荐