JavaScript Strict mode

Source: Internet
Author: User

The "Use strict" directive "use strict" is added in JavaScript 1.8.5 (ECMASCRIPT5). It is not a statement, but it is a literal expression that is ignored in older versions of JavaScript. The purpose of the "use strict" is to specify that the code executes under strict conditions. You cannot use undeclared variables in strict mode.
Strict mode declares strict mode by adding "use strict" to the head of a script or function; An expression to declare. You can view it through the F12 of your browser.

For example, we use the following notation to make an error.
"Use strict"; x = 3.14;       Error (x not defined)

"Use strict"; myFunction (); function myFunction () {    y = 3.14;   Error (y not defined)}

Declaring inside a function is a local scope (using strict mode only within a function) is not an error.
x = 3.14;       Do not error myFunction (); function myFunction () {   "use strict";    y = 3.14;   Error (y not defined)}

Speaking of which, what do we do with strict mode? Summing up the main points are as follows:-Eliminate some of the JavaScript grammar unreasonable, not rigorous, reduce some strange 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. Since the 2014 Script 6 interview, the major browser vendors have joined the support SCRIPT6, and now the front-end of the new technology also to 6-based support, if you want to learn script can learn the next Nanyi teacher click Open Link.

Instance strict mode uses flags to place "use strict" in the first line of the script file, and the entire script will run in "strict mode." If the line statement is not in the first row, it is not valid and the entire script runs in normal mode.
"Use strict";
There are two ways of strict mode invocation,
<script> "Use strict"; Console.log ("This is strict mode.  "); </script> <script> Console.log ("This is normal mode.) "); kly, it ' s almost 2 years ago now.  I can admit it now-i run it on my school's network that have about the computers. </script>
For a single function call,
function Strict () {"Use strict"; Return "This is strict mode.  "; } function Notstrict () {return ' this is normal mode.  "; }
Workarounds for Scripts
(function () {"Use strict"; Some code here}) ();

One feature of the script's statically bound JavaScript language is that it allows "dynamic binding", which is the object that some properties and methods belong to, not determined at compile time, but at runtime (runtime). Strict mode has some restrictions on dynamic binding. In some cases, only static bindings are allowed. That is, the property and method belong to which object, which is determined at the compile stage. Based on the syntax features above,
The WITH statement is forbidden because the WITH statement cannot determine at compile time which object the property belongs to.
"Use strict";  var v = 1;  With (o) {//syntax error v = 2; }
Create eval scope Normal mode, the JavaScript language has two variables scope (SCOPE): global scope and function scope. Strict mode creates a third scope: the scope of the eval.
In normal mode, the scope of the Eval statement depends on whether it is in the global scope or at the function scope. In strict mode, the Eval statement itself is a scope that can no longer generate global variables, and it generates variables that can only be used inside the eval.

"Use strict";  var x = 2; Console.info (eval ("var x = 5; X ")); 5 console.info (x); 2

Strict mode security prevents the This keyword from pointing to a global object
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.
When using a constructor, if you forget that adding new,this no longer points to the global object, it is an error
function f () {"Use strict";  THIS.A = 1;  }; f ();//error, this is undefined

Disallow traversal of the call stack inside a function

Function F1 () {"Use strict"; F1.caller; Error f1.arguments; Error} F1 ();

Disallow deletion of variables
"Use strict";  var x; Delete x;  Syntax error var o = object.create (null, {' x ': {value:1, configurable:true}}); Delete o.x; Delete Succeeded
The function must declare that the new version of JavaScript in the top level will introduce a "block-level scope". In order to conform to the new version, strict mode only allows functions to be declared at the top level of the global scope or function scope. That is, it is not allowed to declare a function within a block of code that is not a function.
"Use strict"; if (true) {function f () {}//Syntax error} for (var i = 0; i < 5; i++) {function f2 () {}//Syntax error}

Prohibit use of Arguments.callee can no longer anonymous internal calls itself
"Use strict";  var f = function () {return arguments.callee;}; f (); Error

OK, so much for the strict mode of JavaScript 6, to be interested in welcoming Dabigatran (278792776).



JavaScript 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.