The method of rewriting window objects is not a novelty. For example, we may need to change the behavior of the default alert. How to rewrite it safely?
Xiaocai saw that a well-known IT website was written like this:
The code copy is as follows:
window.alert = function(){};
or
The code copy is as follows:
alert = function(){};
In fact, this writing is somewhat inappropriate. This is equivalent to adding an alert attribute to the window object. Its priority is higher than the built-in alert in the system, so it can achieve the effect of rewriting, but this is easy to break through. If you execute the following statement, you will restore the alert.
The code copy is as follows:
delete window.alert;
Because the alert rewritten in this way is just an attribute of the window object and can be deleted through the delete operator.
How can it be permanently rewritten and irreversible?
Just define a global variable! Although global variables will also be registered as a property of a window object, they cannot be deleted and they actually exist absolutely. The code is as follows:
The code copy is as follows:
var alert = function(){};
This rewrite method will never be restored and will be safe and reliable!