Javascript 如何覆盖另一个javascript文件中的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4814550/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:13:51  来源:igfitidea点击:

How to override a function in another javascript file?

javascriptoverriding

提问by Jishnu A P

I have a JavaScript file, Mybasefile.js, which has the function Mybasefunction(). I want to override this function in another JavaScript file. When the function is called in a button click, I want the original Mybasefunction()to be executed along with some other code. How can I do this?

我有一个 JavaScript 文件Mybasefile.js,它具有Mybasefunction(). 我想在另一个 JavaScript 文件中覆盖这个函数。当在按钮单击中调用该函数时,我希望原始Mybasefunction()代码与其他一些代码一起执行。我怎样才能做到这一点?

回答by Horia Dragomir

place this code in the override file. Make sure the override file is included after the orginal one.

将此代码放在覆盖文件中。确保覆盖文件包含在原始文件之后。

var orig_Mybasefunction = window.Mybasefunction;
window.Mybasefunction = function(){
    orig_Mybasefunction();
    ...
}