Home  >  Article  >  Web Front-end  >  Detailed explanation of core JavaScript built-in global object/function instances

Detailed explanation of core JavaScript built-in global object/function instances

伊谢尔伦
伊谢尔伦Original
2017-07-27 16:50:301384browse

Core JavaScript built-in objects, that is, objects provided by the ECMAScript implementation that do not depend on the host environment

These objects already exist (instantiated) before the program is executed. ECMAScript is called The Global Object, which is divided into the following types

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

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

3, Constructor Properties of the Global Object (Constructor Properties of the Global Object). There are 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 static classes in Java and can be used directly with the class name + period + method name. There is Math, JSON.

The ECMAScript specification mentions that these global objects (The Global Object) have the Writable attribute, that is, Writable is true, and the enumerability (Enumerable) is false, that is, for in enumeration cannot be used. ECMAScript has this section

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

Although the specification mentions that The Global Object can be rewritten, no one will rewrite them. This is just a test.

NaN    = 11; 
eval   = 22; 
Object = 33; 
Math   = 44; 
alert(NaN); 
alert(eval); 
alert(Object); 
alert(Math);<BR>

Respectively take the global object of the value attribute, the global object of the function attribute, the global object of the constructor (class) attribute, and the global object of other attributes NaN, eval, Object, Math. The results are as follows

The results show that except NaN which cannot be rewritten in IE9(pre3)/Safari, everything else has been rewritten. Here are just four. If you are interested, you can test all the above The Global Objects one by one. The point here is that core JavaScript built-in objects can generally be overridden, although no one does.

The following tests 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); 
}

No pop-up occurs in all browsers, that is, the properties are not enumerated. If you are interested, you can test the enumeration properties of all the above The Global Objects one by one. Of course, for some browsers such as Firefox, some Global Objects can be enumerated after being rewritten.

The above is the detailed content of Detailed explanation of core JavaScript built-in global object/function instances. 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