最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

overriding - how to overwrite a builtin method of javascript native objects - Stack Overflow

programmeradmin0浏览0评论

Lets say we have alert method of window object. I would like to enhance it with nice alertbox.

Also I want to save the existing alert method so that we can switch back once our application is over.

Something like this, but its throwing error in firefox console.

window.prototype.alert = function(){

}

Lets say we have alert method of window object. I would like to enhance it with nice alertbox.

Also I want to save the existing alert method so that we can switch back once our application is over.

Something like this, but its throwing error in firefox console.

window.prototype.alert = function(){

}
Share Improve this question edited Jan 18, 2012 at 11:50 Milad Naseri 4,1181 gold badge29 silver badges40 bronze badges asked Jan 18, 2012 at 11:45 indianwebdevilindianwebdevil 5,1277 gold badges40 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

There is no window.prototype object. window is a global object of javascript context and it is not created from the prototype.

However, what you want to do is achievable with the following code:

window.old_alert = window.alert;  
window.alert = function(txt) {
      // do what you need
      this.old_alert(txt);
}

You can;

var base = window.alert;
window.alert = function(message) {
    document.getElementById("myalertwidget").innerHTML = message;
    return base.apply(this, arguments);
};
发布评论

评论列表(0)

  1. 暂无评论