且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加另一个js文件中存在的方法的事件侦听器

更新时间:2023-11-03 19:48:28

仅当函数具有相同的作用域(全局作用域是***情况)时,您才能执行此操作.如果autoexecute函数具有本地作用域,则无法执行此操作.

You can only do this if the functions have the same scope (global scope is the best case scenario). If the autoexecute function has local scope then you cannot to do it.

本质上,像这样覆盖原始功能...

In essence, override the original function like this...

// keep a reference to the original function
var _autoexecute = autoexecute;

// override the original function with your new one
function autoexecute(d) {
    alert("before autoexecute");  // fired before the original autoexecute
    _autoexecute(d);              // call the original autoexecute function
    alert("after autoexecute");   // fired after the original autoexecute
}

现在,每当调用autotexecute时,它将调用您的新函数,该函数可以处理事件之前和之后,以及调用原始函数.只需删除(可怕的)警报,然后根据需要替换为事件处理程序即可.

Now, whenever autotexecute is called it will call your new function which can handle both before and after events, as well as calling the original function. Just remove the (horrible) alerts and replace with event handlers as required.