Advanced JavaScript---Strict mode

Source: Internet
Author: User

Strict mode (JavaScript)

Strict mode is a way to introduce better error checking into your code.restrictions on Code in Strict Mode sections later in this topic. " The restrictions listed in the limitations section of code in strict mode later in this topic are limited.  ecmascript Language specification, 5th Edition. " > For more information on strict mode, see the  ecmascript Language Specification version 5.

Warning

The version of Internet Explorer prior to Internet Explorer 10 does not support strict mode.

declaring strict mode

Can be added by the beginning of a file, program, or function"Use strict"; to declare strict mode. Such declarations are referred to as "instruction preamble".The scope of a strict mode declaration depends on its context.If strict mode is declared in the global context (outside the scope of the function), all code in the program is in strict mode. If you declare strict mode in a function, all the code in the function is in strict mode. For example, in the following example, all code is in strict mode, and a variable declaration outside the function causes a syntax error "undefined variable in strict mode".

Javascript
"Use strict"; function TestFunction () {    var testvar = 4;    return TestVar;} This causes a syntax Error.testvar = 5;

In the following example, only the code in TestFunction is in strict mode. a variable declaration outside a function does not cause a syntax error, but a declaration inside a function causes a syntax error.

Javascript
function TestFunction () {    "use strict";    This causes a syntax error.    TestVar = 4;    return TestVar;} TestVar = 5;
restrictions imposed by code in strict mode

The following table lists the most important restrictions that apply in strict mode.

Language elements

Limit

Error

Example

variable

Use variables but not declarations.

script5042: variable not defined in strict mode

javascript 
TestVar = 4; 

read-only attribute

writes to the read-only property.

script5045: No assignment to read-only properties in strict mode

javascript 
 var testobj = object.defineproperties ({}, {prop1: {value:10, Writable:false By default}, Prop2: {get:function () {}}}); testobj.prop1 = 20;
TESTOBJ.PROP2 = +; 

Non-extensible properties

Adds an attribute to an object with the extensible property set to False.

SCRIPT5046: Cannot create properties for non-extensible objects

Javascript
var testobj = new Object (); object.preventextensions (testobj); testobj.name = "Bob";

Delete

Delete a variable, function, or parameter.

Remove The property that has the configurable attribute set to False.

SCRIPT1045: not allowed for < expressions > call Delete in strict mode

Javascript
var TestVar = 15;function TestFunc () {};d elete Testvar;delete testfunc;object.defineproperty (testobj, "TestVar", {    Value:10,    configurable:false    });d elete Testobj.testvar;

Repeating properties

Defines a property more than once in an object literal.

SCRIPT1046: A property is not allowed to have more than one definition in strict mode

Javascript
var testobj = {    prop1:10,    prop2:15,    prop1:20};

Duplicate parameter name

Use a parameter name more than once in a function.

SCRIPT1038: Formal parameter name duplication is not allowed in strict mode

Javascript
function TestFunc (param1, param1) {    return 1;};

Future reserved Keywords

Use future reserved keywords as variables or function names.

SCRIPT1050: Cannot use future reserved words for identifiers. The identifier name is reserved in strict mode.

  • implements

  • interface

  • package

  • private

  • protected

  • public

  • static

  • yield

Octal number

Assigns octal values to numeric literals, or attempts to use escape for octal values.

SCRIPT1039: Octal numeric parameters and escape characters are not allowed in strict mode

Javascript
var testoctal = 010;var Testescape = \010;

This

When the value of this is null or undefined, the value is not converted to a global object.

javascript 
 function TestFunc () {return this;} var TestVar = TestFunc (); 

testvar is the global object, but in strict mode the value is undefined." In non-strict mode, the value of the testvar  is a global object, but in strict mode, the value is   undefined.

eval as an identifier" > as identifier of   eval

string" eval "cannot be used as an identifier (variable or function name, parameter name, etc.).

  javascript 
 var eval = ten; 

statement or function declared in a block

You cannot declare a function in a statement or block.

script1047: In strict mode, a function declaration cannot be nested within a statement or block.   They can only be displayed at the top level or directly in the function body.

jscript 
 var arr = [1, 2, 3, 4, 5];var index = null;for (index in arr) {function
MyFunc () {};} 

Variables declared within the Eval function

If you declare a variable within the Eval function, you cannot use the variable outside of the function.

SCRIPT1041: "eval" usage is not valid in strict mode

javascript 
 eval ("var TestVar = ten"); testvar =; 

eval function. " > Although indirect calculations are allowed, you still cannot use variables declared outside the   eval  function.

javascript 
 var indirecteval = Eval;indirecteval ("var testvar = 10;"); document.write (TestVar); 

arguments as an identifier" > as identifier of arguments

string" Arguments "cannot be used as an identifier (variable or function name, parameter name, etc.).

Arguments within a function

You cannot change the value of a member of a local arguments object.

Javascript
function Testargs (onearg) {    arguments[0] = 20;}

In non-strict mode, you can change thearguments[0]  to change the value of the  onearg  parameter so that   onearg  and  arguments[0]  have a value of 20.  arguments[0] does not affect the value of Onearg, because the arguments object is merely a local copy. " In strict mode, changing the value of the  arguments[0]  does not affect the value of  onearg  because   The arguments  object is just a local copy.

Arguments.callee

Not allowed.

Javascript
function (testint) {    if (testint--= = 0)        return;    Arguments.callee (testint--);}

With

Not allowed.

SCRIPT1037: "With" statement is not allowed in strict mode

Javascript
With (Math) {    x = cos (3);    y = tan (7);}
Source: http://msdn.microsoft.com/zh-cn/library/br230269 (v=vs.94). aspx

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