Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript global object/function instances provided by various browser environments

伊谢尔伦
Release: 2017-07-27 16:54:21
Original
1526 people have browsed it

Global objects/functions provided by the host environment

such as window, alert, setTimeout, document, location, etc., most browsers will restrict their rewriting

window = 55; 
alert(window);
Copy after login

This sentence will cause an error under IE, prompting illegal copying, and the subsequent pop-up box will not be executed. Other browsers still pop up the window when window=55 does not exist.

Rewrite alert again

alert = 55; 
console.log(alert);
Copy after login

IE prompts an error, Firefox/Chrome/Safari/Opera has been rewritten, and you can see the output of 55 from the corresponding console. It can be seen that some browsers do not support rewriting of global objects/functions provided by the host environment, while others can.

The following are two ways to declare global variables

a1 = 11; 
var a2 = 22; 
for(a in window){ 
    if(a=='a1'||a=='a2'){ 
        alert(a) 
    } 
}
Copy after login

The above code will not pop up the information box in IE. In IE, the internal content is as follows

//IE 
with(host_object){//window 
    with(global_object){//Global 
        a1 = 11; 
        var a2 = 22; 
    }    
}
Copy after login

That is, a1 and a2 are As the first one mentioned above, the properties on the Global object provided by the JS engine are not the properties on the window object provided by the second hosting environment. Therefore, a1 and a2 do not exist when for in window in IE. If IE provides a reference to the object Global object, maybe the following code can pop up an information box.

for(a in Global){ 
    if(a=='a1'||a=='a2'){ 
        alert(a) 
    } 
}
Copy after login

The interior of Firefox/Safari/Chrome/Opera probably looks like the following

//Firefox/Safari/Chrome/Opera 
with(host_object){//window 
    a1 = 11; 
    var a2 = 22; 
    with(global_object){//Global 
    }    
}
Copy after login

That is, a1 and a2 are the second type mentioned above, on the global object window provided by the host environment. Attributes. Therefore, when for in window, both a1 and a2 exist, and an information box pops up.

Look at the third-party method of declaring the global variable window.a3 = 33. This shows that a3 is hung on the window as a property of the window, so it can be obtained when for in window in all browsers. a3.

The above is the detailed content of Detailed explanation of JavaScript global object/function instances provided by various browser environments. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!