Javascript Strict mode use strict detailed

Source: Internet
Author: User

1. Overview

In addition to the normal operating mode, ECMAscript 5 adds a second mode of operation: "Strict mode" (strict). As the name implies, this mode allows JavaScript to run under stricter conditions.

2. Why use Strict mode

-Eliminate some of the unreasonable and less rigorous JavaScript syntax, and reduce some of the bizarre behavior;

-Eliminate some of the unsafe code operation, to ensure the security of code operation;

-Improve the efficiency of the compiler, increase the speed of operation;

-Pave the future for new versions of JavaScript.

"Strict mode" embodies JavaScript more reasonable, more secure, more rigorous development direction, including IE 10 mainstream browser, has supported it, many large projects have begun to embrace it fully.

On the other hand, the same code, in "Strict mode", may have different running results; some statements that can be run under normal mode will not work in strict mode. Mastering these elements will help you understand JavaScript more carefully and make you a better programmer.

3. Entry Mark

"Use strict";

4. How to Invoke

4.1 For a single script

<script> "Use strict"; Console.log ("This is strict mode.  "); </script>

4.2 For a single function

function Strict () {"Use strict"; Return "This is strict mode.  "; } function Notstrict () {return ' this is normal mode.  "; }

5. Grammatical and behavioral changes

Strict mode has made some changes to JavaScript's syntax and behavior.

5.1 Explicit declaration of global variables

In normal mode, if a variable is assigned without a declaration, the default is the global variable. Strict mode prohibits this usage, and global variables must be explicitly declared.

"Use strict"; v = 1; Error, v not declared for (i = 0; i < 2; i++) {//error, I not declared}

Therefore, in strict mode, variables must first be declared with the Var command before they are used.

5.2 Disable the This keyword from pointing to global objects

function f () {return!this;    }//returns false because "this" points to the global object, "!this" is false function f () {"Use strict";  return!this; }//Returns True because the value of this is undefined in strict mode, so "!this" is true.

Therefore, when using the constructor, if you forget to add new,this no longer point to the global object, but instead error.

function f () {"Use strict";  THIS.A = 1;  }; f ();//error, this is undefined

5.3 Disable deletion of variables

The variable cannot be deleted in strict mode. Only configurable object properties that are set to True can be deleted.

"Use strict";  var x; Delete x;  Syntax error var o = object.create (null, {' x ': {value:1, configurable:true}}); Delete o.x; Delete Succeeded

5.4 Object cannot have a property with duplicate name

In normal mode, if the object has more than one duplicate attribute, the last assigned value overrides the previous value. In strict mode, this is a syntax error.

"Use strict"; var o = {p:1, p:2}; Syntax error

5.5 function cannot have parameter with duplicate name

In normal mode, if the function has more than one parameter with the same name, it can be read with Arguments[i]. In strict mode, this is a syntax error.

"Use strict";  function f (A, A, b) {//syntax error return; }

Javascript Strict mode use strict detailed

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.