Rewriting the method of the window object is not a novel thing. For example, we may need to change the default alert behavior. How to rewrite it safely?
Xiaocai saw a well-known IT website written like this:
or
Actually, this way of writing is somewhat inappropriate. This is equivalent to adding an alert attribute to the window object. Its priority is higher than the system's built-in alert, so it can achieve the effect of rewriting. However, this is easy to break through. Executing the following statement will restore the alert.
Because the alert rewritten in this way is just an attribute of the window object, it can be deleted through the delete operator.
How can we rewrite it permanently and irreversibly?
Just define a global variable! Although the global variable will also be registered as an attribute of the window object, it cannot be deleted and it absolutely exists in fact. The code is as follows:
This rewriting method will never be restored, it is safe and reliable!