ECMAScript 5 Strict Mode (Strict mode) Introduction _ Basics

Source: Internet
Author: User

Strict mode (Strict mode) is a new feature of ECMAScript 5, which allows you to place the entire program, or a function, in a "strict" operating context. This strict context prevents certain actions from being thrown and throws more exceptions.

Although ECMAScript 5 is backward-compatible for ECMAScript 3, in strict mode, all attributes that are not favoured in ECMAScript 3 are disabled (or throw errors) instead of compatible.

Enabling strict mode has the following benefits:

1. Catch some programming errors and throw exceptions.
2. Block some relatively "unsafe" operations (such as accessing global variables) and throw exceptions.
3. Disable some confusing features.

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

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

How to enable strict mode

Adding this statement at the beginning of the program enables strict mode for the entire script:

Copy Code code as follows:

' Use strict ';

You can also enable strict mode only within a function so that it does not affect the external:
Copy Code code as follows:

function Imstrict () {
' Use strict ';
... your code ...
}

A statement that enables strict mode is just a normal string "use strict" with no new syntax. This means that there is no negative impact on older browsers.

One practical application for enabling strict mode within a function is to define the entire JavaScript class library within a strictly-modal function so that it does not affect external code:

Copy Code code as follows:

Non-strict code ...

(function () {
"Use strict";

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

Non-strict code ...


So what's the change in the strict-mode script?

Variables and properties

Assigning values to undefined variables will fail, rather than taking this variable as a global variable.

Writing a property with a writable attribute of false, deleting a property with a configurable attribute of false, or adding a property with an extensible attribute of false results in an error (these attributes are predetermined). In the past, these operations would not throw an exception, but simply fail silently.

Performing a delete operation on a variable, function, or function argument can cause an error.

Copy Code code as follows:

var foo = ' Test ';
function Test () {}

Delete foo; Error
Delete test; Error

function Test2 (ARG) {
Delete arg; Error
}


Defining the same property within an object container causes the exception to be thrown:
Copy Code code as follows:

Error
{foo:true, Foo:false}

Eval

Any use of the name "eval" (the main intent is to point the Eval function to a variable or to an object's properties) is prohibited.

Copy Code code 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 a new variable through eval can also be invalid:
Copy Code code as follows:

Eval ("var a = false;");
Print (typeof a); Undefined

Function

Overriding the arguments object can result in an error:

Copy Code code as follows:

Arguments = [...]; Not allowed

A parameter with the same name can cause an error:
Copy Code code 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 first be named, for example:
Copy Code code as follows:

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

The arguments, caller, and callee properties of a function no longer exist, and the operations that define them are also prohibited.
Copy Code code as follows:

function Test () {}
Test.caller = ' caller '; Error

At last A long-standing (and very annoying) bug has been resolved: when null or undefined is used as the first argument of the Function.prototype.call or Function.prototype.apply method, this within the function points to the global object. Strict mode prevents it from executing and throws an exception:
Copy Code code as follows:

(function () {...}). Call (NULL); Exception

With () {}

The WITH () {} statement is completely hung out in strict mode.

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.