Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to overwrite a builtin method of javascript native objects

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(){

}
like image 632
indianwebdevil Avatar asked Feb 21 '23 19:02

indianwebdevil


1 Answers

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);
}
like image 92
Krizz Avatar answered Apr 09 '23 17:04

Krizz