javascript - 有没有办法克隆 javascript 中的 native 函数,例如 window.alert 或 document.write

标签 javascript function clone native-methods

我想要做的是将代码中的每个警报更改为自定义警报(“您使用过警报”);

var hardCodedAlert = alert; //I know this won't work.But what to do ?
window.alert=function(){
if(this.count == undefined)
this.count=0;
this.count=this.count+1;
if(this.count == 1)
hardCodedAlert("You used alert");
};

最佳答案

是的,你可以这样做(例如):

var oldalert = window.alert;
window.alert= function alert(t){
  alert.count = !alert.count ? 1 : alert.count + 1;
  oldalert(t+' - That\'s alert nr '+alert.count);
};

参见this jsfiddle

关于javascript - 有没有办法克隆 javascript 中的 native 函数,例如 window.alert 或 document.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10266825/

相关文章:

vector - 如何将向量复制到另一个位置并重用现有的分配内存?

c++ - 实现可克隆 C++ 类的最佳方式?

java - Cloneable 抛出 CloneNotSupportedException

javascript - map 内部的三元组不返回结果

javascript - JqG​​rid 检查行是否被选中

javascript - 单击 onclick 单元格滚动到顶部

javascript - Selenium 和异步 JavaScript 调用

javascript - 如何告诉 Passport JS 中的 typescript req.user 永远不会被定义?

c - 指向函数代码结尾的指针

c++ - c++ 中是否有一个方法/函数,后来的常量参数基于第一个参数?