Home > Web Front-end > JS Tutorial > A brief discussion on the method of rewriting the window object_Basic knowledge

A brief discussion on the method of rewriting the window object_Basic knowledge

WBOY
Release: 2016-05-16 16:23:38
Original
1388 people have browsed it

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:

Copy code The code is as follows:

window.alert = function(){};

or

Copy code The code is as follows:

alert = function(){};

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.

Copy code The code is as follows:

delete window.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:

Copy code The code is as follows:

var alert = function(){};

This rewriting method will never be restored, it is safe and reliable!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template