Home > Article > Web Front-end > What are the differences between javascript strict mode
Differences: 1. It is forbidden to use the with statement; 2. It is forbidden to point the this keyword to the global object; 3. It is forbidden to traverse the call stack inside the function; 4. Objects cannot have duplicate names, and functions cannot have duplicate names. Parameters; 5. Octal notation is prohibited; 6. Assignment of arguments is not allowed; 7. Functions are not allowed to be declared in non-function code blocks.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Strict mode has made some changes to the syntax and behavior of Javascript.
1 Explicit declaration of global variables
In normal mode, if a variable is assigned a value without being declared, the default is a global variable. Strict mode prohibits this usage and global variables must be declared explicitly.
"use strict"; v = 1; // 报错,v未声明 for(i = 0; i < 2; i++) { // 报错,i未声明 }
Therefore, in strict mode, variables must be declared with the var command before use.
2 Static binding
One of the characteristics of the Javascript language is that it allows "dynamic binding", that is, certain properties and methods belong to Which object is determined not at compile time, but at runtime.
Strict mode places some restrictions on dynamic binding. In some cases, only static binding is allowed. In other words, which object the properties and methods belong to is determined during the compilation stage. This will help improve compilation efficiency, make the code easier to read, and cause fewer surprises.
Specifically, it involves the following aspects.
(1) It is forbidden to use the with statement
Because the with statement cannot determine at compile time which object the attribute belongs to.
"use strict"; var v = 1; with (o){ // 语法错误 v = 2; }
(2) Create eval scope
In normal mode, the Javascript language has two variable scopes (scope): global scope and function scope. Strict mode creates a third scope: eval scope.
In normal mode, the scope of the eval statement depends on whether it is in the global scope or the function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables. The variables it generates can only be used inside eval.
"use strict"; var x = 2; console.info(eval("var x = 5; x")); // 5 console.info(x); // 2
3 Enhanced security measures
(1) Prohibit this keyword from pointing to the global object
function f(){ return !this; } // 返回false,因为"this"指向全局对象,"!this"就是false function f(){ "use strict"; return !this; } // 返回true,因为严格模式下,this的值为undefined,所以"!this"为true。
Therefore, when using the constructor, if you forget to add new, this will no longer point to the global object, but an error will be reported.
function f(){ "use strict"; this.a = 1; }; f();// 报错,this未定义
(2) It is forbidden to traverse the call stack inside the function
function f1(){ "use strict"; f1.caller; // 报错 f1.arguments; // 报错 } f1();
4 It is forbidden to delete variables
Variables cannot be deleted in strict mode. Only object properties with configurable set to true can be deleted.
"use strict"; var x; delete x; // 语法错误 var o = Object.create(null, {'x': { value: 1, configurable: true }}); delete o.x; // 删除成功
5 Explicit error reporting
In normal mode, when assigning a value to a read-only property of an object, no error will be reported, only silently failed. In strict mode, an error will be reported.
"use strict"; var o = {}; Object.defineProperty(o, "v", { value: 1, writable: false }); o.v = 2; // 报错
In strict mode, an error will be reported when assigning a value to an attribute read using the getter method.
"use strict"; var o = { get v() { return 1; } }; o.v = 2; // 报错
In strict mode, adding new attributes to objects that are prohibited from expansion will result in an error.
"use strict"; var o = {}; Object.preventExtensions(o); o.v = 1; // 报错
In strict mode, deleting an attribute that cannot be deleted will result in an error.
"use strict"; delete Object.prototype; // 报错
6 Duplicate name error
Strict mode has added some new syntax errors.
(1) The object cannot have attributes with the same name
In normal mode, if the object has multiple attributes with the same name, the last assigned attribute will overwrite the previous one. value. In strict mode, this is a syntax error.
"use strict"; var o = { p: 1, p: 2 }; // 语法错误
(2) The function cannot have parameters with the same name
In normal mode, if the function has multiple parameters with the same name, you can use arguments[i] to read them Pick. In strict mode, this is a syntax error.
"use strict"; function f(a, a, b) { // 语法错误 return ; }
7 Prohibit octal representation
In normal mode, if the first digit of an integer is 0, it means that it is an octal number, such as 0100 is equal to 64 in decimal. Strict mode prohibits this representation, the first bit of the integer is 0, and an error will be reported.
"use strict"; var n = 0100; // 语法错误
8 Restrictions on the arguments object
arguments is the parameter object of the function, and strict mode restricts its use.
(1) Assignment to arguments is not allowed
"use strict"; arguments++; // 语法错误 var obj = { set p(arguments) { } }; // 语法错误 try { } catch (arguments) { } // 语法错误 function arguments() { } // 语法错误 var f = new Function("arguments", "'use strict'; return 17;"); // 语法错误
(2) arguments no longer tracks parameter changes
function f(a) { a = 2; return [a, arguments[0]]; } f(1); // 正常模式为[2,2] function f(a) { "use strict"; a = 2; return [a, arguments[0]]; } f(1); // 严格模式为[2,1]
(3) It is prohibited to use arguments.callee
This means that you cannot call itself inside an anonymous function.
"use strict"; var f = function() { return arguments.callee; }; f(); // 报错
9 Functions must be declared at the top level
In the future, new versions of Javascript will introduce "block-level scope". In order to keep in line with the new version, strict mode only allows functions to be declared in the global scope or the top level of the function scope. That is, it is not allowed to declare functions within a non-function code block.
"use strict"; if (true) { function f() { } // 语法错误 } for (var i = 0; i < 5; i++) { function f2() { } // 语法错误 }
【相关推荐:javascript学习教程】
The above is the detailed content of What are the differences between javascript strict mode. For more information, please follow other related articles on the PHP Chinese website!