【发布时间】:2021-08-27 09:42:36
【问题描述】:
我有一个引导模式,它有一些自定义事件,比如 hidden.bs.modal,取决于用户在哪里做,我希望这个事件中的函数被替换,也许用一个简单的例子来理解会更好,考虑:
const currentModal; // imagine an any modal here.
window.addEventListener('load', () => {
currentModal.addEventListener('hidden.bs.modal', standardFunction );
});
function standardFunction(){
alert('hi there');
// this is standard output to modal closed
}
function buttonClickedChange(){
// Here, i need override standardFunction
this.standardFunction = function(){
alert('modal event hidden.bs.modal changed with success!');
// this must be override previous output
};
}
无论函数的重新声明如何,方法的输出仍然是标准的,这是因为事件监听器不引用存储的函数,而只是“复制”其内容并仅在内部创建其范围。
【问题讨论】:
标签: javascript dom dom-events