Javascript Strict mode details (top)

Source: Internet
Author: User

ECMAScript 5 was the first to introduce the concept of "strict mode" (strict). Strict mode allows you to select a more rigorous global or local error condition detection within the function. The advantage of using strict mode is that you can know the errors in your code early and catch some ECMAScript behavior that could lead to errors. Browsers that support strict mode include IE 10+,firefox 4+,safari 5.1+ and Chrome.

1. Select Use

To enter strict mode, you can use strict-mode compilation instructions (pragma)

"Use strict";

If this compilation instruction is given in the global scope (outside the function), the entire script will use strict mode.

Or just open strict mode in the function, just like this:

function dosomething () {       "use strict";      // other Code }

If you don't have the right to control all the scripts in the page, you only need to turn on strict mode in the specific function you are testing.

2. Variables

In strict mode, there is a limit as to when to create variables and how to create them. First, you are not allowed to accidentally create global variables. In non-strict mode, you can create global variables like this:

// variable not declared // non-strict mode: Create global variables // Strict mode: Throw Referenceerror message= "Hello world!";

Even if you do not have the Var keyword in front of the message, you can create a message as a global variable even if it is not defined as a property of a global object. In strict mode, however, if you assign a value to a variable that is not declared, the code throws a referenceerror when it executes.

Second, you cannot call the delete operator on a variable. Non-strict mode allows this operation, but silently fails. In strict mode, deleting a variable also results in an error.

// Delete a variable // non-strict mode: Silent failure // Strict mode: Throw Referenceerror var color= "Red"; delete color;

There are also restrictions on variable names in strict mode. In particular, you cannot use implements,interface,let,package,private,protected,public,static and yield as variable names. In strict mode, using the above identifier as the variable name results in a syntax error.

3. Objects

Manipulating objects in strict mode is more likely to cause errors than in non-strict mode. In general, in the case of silent failure in non-strict mode, an error is thrown in strict mode. Therefore, using strict patterns in development increases the likelihood of early detection of errors.

Manipulating the properties of an object causes an error in the following situations:

    • Assigning a value to a read-only property throws a typeerror;
    • Using the delete operator on a non-configurable (nonconfigurable) property throws TypeError;
    • Adding a property to an object that is not extensible (nonextensible) throws TypeError.

Another restriction on using objects is related to declaring objects by object literals. Property names must be unique when using object literals.

For example:

// Duplicate name attribute // non-strict mode: No error, whichever is the second attribute // Strict mode: throws a syntax error var person={                name:"Nicholas",                Name:"Greg"           };

4. Functions

First, strict mode requires that the parameters of a named function must be unique. Take the following function as an example:

// Duplicate Parameter // non-strict mode: no error, only the second parameter can be accessed // Strict mode: throws a syntax error function sum (num,num) {                         //dosonmething}

In non-strict mode, this function declaration does not throw an error. Only the second parameter can be accessed through the parameter name, and the first argument must be accessed through the arguments object.

In strict mode, the behavior of the arguments object is also different, in non-strict mode, the value of the modified named parameter is also reflected in the argument object, while in strict mode the two values are completely independent. For example:

// Modifying the value of a named parameter // non-strict mode: changes are reflected in the argument // Strict mode: Modifications are not reflected in the argument function Showvalue (value) {       value= "Foo";       alert (value);              // "Foo"       Alert (argument[0]);  // non-strict mode: "Foo"                                    // Strict mode: "Hi" }showvalue ("Hi");

Another change is the elimination of Arguments.callee and Arguments.caller. In non-strict mode, these two properties refer to the function itself, a reference to the calling function. In strict mode, which property is accessed will throw typeerror. For example:

// Visit Arguments.callee // non-strict mode: no problem // Strict mode: Throw TypeError function factorial (num) {     if(num<=1) {              return;     } Else {              return Num*arguments.callee (num-1)     }}var result= Factorial (5);

Similarly, attempting to read and write the caller property of a function also causes the TypeError to be thrown. So, for the above example, accessing Factorial.caller also throws an error.

Similar to variables, strict mode restricts function names and does not allow implements,interface,let,package,private,protected,public,static and yield as function names.

The last limitation on a function is that it can only be declared at the top level of the script and inside the function, that is, declaring a function in an if statement results in a syntax error:

// declaring a function in an if statement // non-strict mode: Promote a function outside the IF statement // Strict mode: throws a syntax error if (true) {     function  dosomething () {               //... ..      }}

In non-strict mode, the above code can be run in all browsers, but in strict mode it causes a syntax error.

Javascript Strict mode details (top)

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.