Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences in javascript strict mode?

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what are the differences in the strict mode of javascript. I hope you will gain something after reading this article. Let's discuss it together.

Differences: 1, prohibit the use of the with statement; 2, prohibit the this keyword pointing to the global object; 3, prohibit traversing the call stack within the function; 4, the object can not have duplicate attribute, the function can not have duplicate parameters; 5, prohibit octal representation; 6, do not allow assignment to arguments; 7, do not allow the declaration of functions in non-functional blocks of code.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The difference of javascript strict mode

Strict mode has made some changes to the syntax and behavior of Javascript.

1 the global variable is explicitly declared

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

"use strict"; v = 1; / / error reported, v does not declare for (I = 0; I < 2; iTunes +) {/ / error, I undeclared}

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

2 static binding

One feature of the Javascript language is that it allows "dynamic binding", that is, which object certain properties and methods belong to is determined not at compile time, but at runtime time.

Strict mode imposes some restrictions on dynamic binding. In some cases, only static binding is allowed. That is, the object to which the properties and methods belong is determined during the compilation phase. This improves compilation efficiency and makes the code easier to read and less unexpected.

Specifically, it involves the following aspects.

(1) prohibit the use of with statement

Because the with statement cannot determine which object the attribute belongs to at compile time.

"use strict"; var v = 1; with (o) {/ / syntax error 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 an eval statement depends on whether it is in global scope or in function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables, and the variables it generates can only be used within eval.

"use strict"; var x = 2; console.info (eval ("var x = 5; x"); / / 5 console.info (x); / / 2

3 enhanced security measures

(1) prohibit 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 in strict mode, the value of this is undefined, so "! this" is true.

Therefore, when using the constructor, if you forget to add new,this, it no longer points to the global object, but reports an error.

Function f () {"use strict"; this.a = 1;}; f (); / / error, this is not defined

(2) it is prohibited to traverse the call stack inside the function.

Function F1 () {"use strict"; f1.caller; / / error f1.arguments; / / error} F1 ()

4 prohibit deletion of variables

Variables cannot be deleted in strict mode. Can be deleted only if configurable is set to the object property of true.

"use strict"; var x; delete x; / / Syntax error var o = Object.create (null, {'x error: {value: 1, configurable: true}}); delete o.x; / / deleted successfully

5 explicit error report

In normal mode, assigning a read-only property of an object will not report an error, but will fail silently. In strict mode, an error will be reported.

"use strict"; var o = {}; Object.defineProperty (o, "v", {value: 1, writable: false}); o.v = 2; / / error report

In strict mode, an error is reported when assigning an attribute that is read using the getter method.

"use strict"; var o = {get v () {return 1;}}; o.v = 2; / / error report

In strict mode, adding a new property to an object that forbids extension will report an error.

"use strict"; var o = {}; Object.preventExtensions (o); o.v = 1; / / error report

In strict mode, deleting an attribute that cannot be deleted will report an error.

"use strict"; delete Object.prototype; / / error report

6 duplicate name error

Some syntax errors have been added to strict mode.

(1) objects cannot have attributes with duplicate names.

In normal mode, if the object has more than one property with the same name, the last property assigned will overwrite the previous value. In strict mode, this is a grammatical error.

"use strict"; var o = {p: 1, p: 2}; / / syntax error

(2) function cannot have parameters with duplicate names.

In normal mode, if the function has more than one argument with the same name, it can be read with arguments [I]. In strict mode, this is a grammatical error.

"use strict"; function f (a, a, b) {/ / syntax error return;}

7 prohibition of octal representation

In normal mode, if the first digit of an integer is 0, it means that this is an octal number, for example, 0100 equals 64 in decimal system. Strict mode forbids this representation. If the first integer bit is 0, an error will be reported.

"use strict"; var n = 0100; / / syntax error

8 limitations of arguments object

Arguments is the parameter object of a function, and strict mode limits its use.

(1) arguments assignment is not allowed

"use strict"; arguments++; / / Syntax error var obj = {set p (arguments) {}}; / / Syntax error try {} catch (arguments) {} / / Syntax error function arguments () {} / / Syntax error var f = new Function ("arguments", "'use strict'; return 17;"); / / syntax error

(2) arguments no longer tracks changes in parameters.

Function f (a) {a = 2; return [a, arguments [0]];} f (1); / / normal mode is [2 function f (a) {"use strict"; a = 2; return [a, arguments [0]];} f (1); / / strict mode is [2j1]

(3) prohibit the use of arguments.callee

This means that you can't call yourself inside an anonymous function.

"use strict"; var f = function () {return arguments.callee;}; f (); / / error report

9 functions must be declared at the top level

Block-level scope will be introduced in future versions of Javascript. To be in line with 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) Javascript +) {function f2 () {} / / grammatical error} after reading this article, I believe you have a certain understanding of "what are the differences in the strict mode of javascript". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 243

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report