Home  >  Article  >  Web Front-end  >  Detailed explanation of JavaScript global object/function instances provided by various browser environments

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

伊谢尔伦
伊谢尔伦Original
2017-07-27 16:54:211549browse

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);

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);

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) 
    } 
}

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; 
    }    
}

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) 
    } 
}

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 
    }    
}

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!

Statement:
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