A brief analysis of two types of global objects and functions in JavaScript

  • 2020-03-30 00:44:00
  • OfStack

By JavaScript, I mean the browser environment, including the host environment. The first is ECMAScript Global Object, and the second is the Global Object/function under the Host environment (Host).

I. core JavaScript built-in objects, that is, objects not dependent on the host environment provided by the ECMAScript implementation

These objects exist (instantiated) before the program executes. ECMAScript is called The Global Object and is divided into The following categories

1. Value Properties of the Global Object. NaN, Infinity, undefined.

2. Function Properties of the Global Object. Eval, parseInt, parseFloat, isNaN, isFinite, decodeURI, encodedURI, encodeURIComponent

Constructor Properties of the Global Object. Object, Function, Array, String, Boolean, Number, Date, RegExp, Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError.

4. Other Properties of the Global Object can be seen as a static class in Java, which can be directly used by the class name + dot + method name. Math, JSON.

The ECMAScript specification mentions that these Global objects are Writable, which means that Writable is true and Enumerable is false, which means that for in enumeration cannot be used. ECMAScript has this paragraph

Unless otherwise specified, the standard built - in the properties of the global object have the attributes {[[Writable]] : true, [[Enumerable]] : false, the [[Configurable]] : true}.


Although The specification mentions that The Global objects can be overridden, no one is going to overwrite them. This is just a test.


NaN    = 11; 
eval   = 22; 
Object = 33; 
Math   = 44; 

alert(NaN); 
alert(eval); 
alert(Object); 
alert(Math);<BR> 

Value the global Object of a property, the global Object of a function property, the global Object of a constructor (class) property, and the global Object of other properties NaN, eval, Object, Math. The results are as follows

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201312/20131205095826.png ">

The results show that NaN is overridden except in IE9(pre3)/Safari. There are only four of them listed here. Those who are interested can test all of The above Global objects one by one. The point here is that core JavaScript built-in objects can generally be overridden, although no one does.

Let's test its enumerability


for(var a in NaN){ 
    alert(a); 
} 
for(var a in eval){ 
    alert(a); 
} 
for(var a in Object){ 
    alert(a); 
} 
for(var a in Math){ 
    alert(a); 
} 

All browsers do not pop up, that is, properties are not enumerated. Those interested can test The enumerability of all The Global objects above. Of course, for some browsers, such as Firefox, some Global objects can be overridden and enumerated.

Global objects/functions provided by the host environment

Such as Windows, alert, setTimeout, document, such as the location, most browsers will limit the rewrite


window = 55; 
alert(window); 

This sentence in IE will error prompt illegal copy, the popup box did not execute. Other browsers pop up Windows when window=55 doesn't exist.

Here 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 in IE message box, in IE inside the following

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

A1, a2 is a property on the Global object provided by the JS engine in the first category, not on the window object provided by the second hosting environment. So a1 and a2 don't exist in IE for in window. If a reference to the object Global object is provided in IE, maybe the following code will pop up a message box.

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

Firefox/Safari/Chrome/internal below is pretty much what's in the Opera

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

A1,a2 is a property on the global object window provided by the host environment as mentioned above. So for in window, both a1 and a2 exist, and that pops up.

The third way to declare the global variable window.a3 = 33 is to display that a3 is hung on the window as a property of the window, so that a3 is available in all browsers for in window.


Related articles: