有没有一种方法克隆JavaScript中的本地函数,如window.alert或document.write [英] Is there a way to clone native functions in javascript like window.alert or document.write

查看:152
本文介绍了有没有一种方法克隆JavaScript中的本地函数,如window.alert或document.write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是将代码中的每个提醒都改为自定义提醒(您使用提醒);

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


推荐答案

是的,您可以(例如) / p>

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

请参阅这个jsfiddle

这篇关于有没有一种方法克隆JavaScript中的本地函数,如window.alert或document.write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆