ECMAScript5 strict mode introduction _ basic knowledge

Source: Internet
Author: User
This article mainly introduces the strict mode of ECMAScript5 (StrictMode). This article describes how to enable the strict mode and how it affects variables, attributes, functions, and with statements after the strict mode is enabled, for more information, see Strict Mode, which is a new feature of ECMAScript 5. It allows you to place the entire program or a function in a "Strict" operating context. This strict context prevents some specific operations and throws more exceptions.

Although ECMAScript 5 is backward compatible with ECMAScript 3, in strict mode, all features that are not supported in ECMAScript 3 are disabled (or throw an error) rather than compatible.

Enabling strict mode has the following benefits:

1. capture some programming errors and throw exceptions.
2. Block operations that are relatively "insecure" (such as accessing global variables) and throw an exception.
3. Disable some confusing features.

Most information about the strict mode can be found on page 223rd of ES5 specification [PDF.

(Note: The strict mode of ECMAScript 5 is different from that of Firefox)

How to enable strict Mode

Add this statement at the beginning of the program to enable the strict mode for the entire script:

The Code is as follows:


'Use strict ';


You can also enable the strict mode only within the function, which does not affect the External:

The Code is as follows:


Function imStrict (){
'Use strict ';
//... Your code...
}


The statement that enables strict mode is just a common string "use strict" without any new syntax. This means that it will not have any negative impact on legacy browsers.

One practical application of enabling the strict mode inside a function is to define the entire Javascript class library inside the function of the strict mode, so that the external code is not affected:

The Code is as follows:


// Non-strict code...

(Function (){
"Use strict ";

// Define your library strictly...
})();

// Non-strict code...


So what is the change in the script in strict mode?

Variables and attributes

The assignment of undefined variables will fail, rather than using this variable as a global variable.

Write an attribute with the writable property of false, delete an attribute with the configurable property of false, or add an attribute with the Extensible property of false, will lead to errors (these features are pre-agreed ). In the past, these operations did not throw an exception, but only failed silently.

Executing the delete operation on variables, functions, or function parameters may cause errors.

The Code is as follows:


Var foo = 'test ';
Function test (){}

Delete foo; // Error
Delete test; // Error

Function test2 (arg ){
Delete arg; // Error
}


Defining the same attributes inside an object container will cause an exception to be thrown:

The Code is as follows:


// Error
{Foo: true, foo: false}

Eval

Any use of the name "eval" (mainly intended to direct the eval function to a variable or an object attribute) is forbidden.

The Code is as follows:


// All generate errors...
Obj. eval =...
Obj. foo = eval;
Var eval = ...;
For (var eval in ...){}
Function eval (){}
Function test (eval ){}
Function (eval ){}
New Function ("eval ")


In addition, declaring new variables through eval will also be invalid:

The Code is as follows:


Eval ("var a = false ;");
Print (typeof a); // undefined

Function

Overwriting the arguments object will cause an error:

The Code is as follows:


Arguments = [...]; // not allowed


Parameters with the same name may cause errors:

The Code is as follows:


(Function (foo, foo) {}) // Error


Access to arguments. caller and arguments. callee throws an exception. Therefore, any anonymous function that needs to be used must be named first. For example:

The Code is as follows:


SetTimeout (function later (){
// Do stuff...
SetTimeout (later, 1000 );
},1000 );


The arguments, caller, and callee attributes of the function no longer exist. Defining them is also forbidden.

The Code is as follows:


Function test (){}
Test. caller = 'caller'; // Error


Finally, a long-standing (and annoying) BUG has been fixed: When null or undefined is used as the Function. prototype. call or Function. prototype. when the first parameter of the apply method is used, this inside the function will point to the global object. The strict mode will stop execution and throw an exception:

The Code is as follows:


(Function () {...}). call (null); // Exception


With (){}

The with () {} statement is completely suspended in strict mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.