Is there a way to clone native functions in javascript like window.alert or document.write

回眸只為那壹抹淺笑 提交于 2019-12-11 01:51:30

问题


What I want to do is change every alert in the code to a custom alert("you used alert");

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");
};

回答1:


Yes, you can do (for example):

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);
};

See this jsfiddle



来源:https://stackoverflow.com/questions/10266825/is-there-a-way-to-clone-native-functions-in-javascript-like-window-alert-or-docu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!