javascript 从另一个文件覆盖 JS 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19301516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override JS function from another file
提问by Wednesday Man
Im trying to override a JS function from Bigcartel. I have no access to the JS file.
我试图覆盖 Bigcartel 的 JS 函数。我无权访问 JS 文件。
The original is:
原文是:
updateCart: function(cart) {
$('aside .cart .count, .main header .cart').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},
And i am trying to change it to this:
我正在尝试将其更改为:
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));
},
I am aware that others have asked similar questions, but i am a complete noob when it comes to understanding how to implement JS (i only know how to tweek through trial and error)
我知道其他人也问过类似的问题,但在理解如何实现 JS 方面,我完全是个菜鸟(我只知道如何通过反复试验进行调整)
If any one could be so kind as to help me out by giving me the answerthat would be great.
如果有人能这么好心,给我一个答案来帮助我,那就太好了。
Thanks,
谢谢,
iWed-
星期三-
EDIT [10.10.13 :: 21:24hr]
编辑 [10.10.13 :: 21:24 小时]
To clarify, i do not have direct access to the original JS file. i can only view it through chrome. I only have access to html files. It is for a Big cartel theme Edit.
澄清一下,我没有直接访问原始 JS 文件的权限。我只能通过chrome查看。我只能访问 html 文件。这是一个大卡特尔主题编辑。
Here is a link to to copied JS using chrome. Line 216 is the code, if this helps : http://jsfiddle.net/w9GTJ/
这是使用 chrome 复制 JS 的链接。第 216 行是代码,如果这有帮助的话:http: //jsfiddle.net/w9GTJ/
回答by Tibos
EDIT:You are in luck. From the posted code you can see that the updateCart method is exported on the window.Store global object. The solution is to add this code after the original script loaded:
编辑:你很幸运。从发布的代码中您可以看到 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));
};
Explanation for a general situation:
一般情况说明:
All scripts loaded in a web page run in the same global scope, so overwriting a variable is as simple as inserting your script afterwards:
网页中加载的所有脚本都在相同的全局范围内运行,因此覆盖变量就像之后插入脚本一样简单:
<script>
var x = 5; // original script
</script>
<script>
x = 2; // your inserted script
</script>
From the looks of it, your function is defined as property of an object:
从它的外观来看,您的函数被定义为对象的属性:
var x = {
updateCart : function(cart) {
// stuff
}
}
So to overwrite it you need to do:
因此,要覆盖它,您需要执行以下操作:
x.updateCart = function(cart) {
// your code
}
Finally, there is one situation where you simply can't overwrite it, if function is private in the original code:
最后,如果原始代码中的函数是私有的,则有一种情况您根本无法覆盖它:
function() {
var x = {
updateCart: function(){}
}
}()
// No way to access x.updateCart here
回答by fmgp
Assuming you're able to find and access corresponding js object:
假设您能够找到并访问相应的 js 对象:
[theTargetObject].prototype.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));
}