In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "javascript strict mode how to call", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "javascript strict mode how to call" article.
JavaScript strict mode refers to running code under strict conditions, which defines a different parsing and execution model for JavaScript. It will check developers' code errors under more stringent conditions. It can be used globally or locally (applied to the interior of the function).
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
I. what is the strict model?
In ES5, JS introduced the concept of strict mode for the first time. JavaScript strict mode (strict mode) runs under strict conditions.
Strict mode defines a different parsing and execution model for JavaScript, it will check your code errors under more stringent conditions, it can be used globally or locally (applied to the interior of the function). It is necessary to understand the rules of strict mode, and in the future, ECMAScript will gradually force strict mode to be used globally.
As the name implies, this mode allows Javascript to run under more stringent conditions.
The main purposes of establishing a "strict model" are as follows:
-eliminate some unreasonable and imprecise aspects of Javascript syntax and reduce some strange behaviors
-eliminate some unsafe places where the code runs, and ensure the safety of the code.
-improve compiler efficiency and running speed
-pave the way for future versions of Javascript.
"strict mode" reflects the more reasonable, secure and rigorous direction of Javascript. Mainstream browsers, including IE 10, have supported it, and many big projects have begun to embrace it in an all-round way.
On the other hand, the same code may have different results in "strict mode"; some statements that can be run in "normal mode" will not run in "strict mode". Mastering these contents will contribute to a more detailed and in-depth understanding of Javascript and make you a better programmer.
This article will give a detailed introduction to the "strict mode".
II. Entry sign
The sign to enter "strict mode" is the following line:
"use strict"
Older browsers would ignore it as a line of ordinary strings.
Third, how to call
Strict Mode has two invocation methods, which are suitable for different situations.
3.1 for the entire script file
Place "use strict" on the first line of the script file, and the entire script will run in strict mode. If this statement is not on the first line, it is invalid and the entire script runs in "normal mode". This requires special attention if code files from different modes are merged into one file.
(strictly speaking, "use strict" can not be on the first line, such as directly following an empty semicolon, as long as the statement preceding it is not the statement that produces the actual run result.)
"use strict"; console.log ("this is strict mode.") ; 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 has about 50 computers.
The above code indicates that there are two pieces of Javascript code in turn in a web page. The former script tag is in strict mode, while the latter is not.
3.2 for a single function
If you place "use strict" on the first line of the function body, the entire function runs in strict mode.
Function strict () {"use strict"; return "this is strict mode." ;} function notStrict () {return "this is normal mode." ;}
3.3 flexible writing of script files
Because the first invocation method is not good for file merging, it is better to borrow the second method and put the entire script file in an anonymous function that executes immediately.
(function () {"use strict"; / / some code here}) ()
IV. Grammar and behavior changes
Strict mode has made some changes to the syntax and behavior of Javascript.
4.1 Global variables are 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.
4.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
4.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.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
4.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
4.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;}
4.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
4.8 limitations of arguments objects
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
4.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; iTunes +) {function f2 () {} / / Syntax error}
4.10 reserved words
In order to transition to a future version of Javascript, some reserved words have been added to strict mode: implements, interface, let, package, private, protected, public, static, yield.
Using these words as variable names will result in an error.
Function package (protected) {/ / syntax error "use strict"; var implements; / / syntax error}
In addition, the fifth edition of ECMAscript itself also stipulates some other reserved words (class, enum, export, extends, import, super), as well as const reserved words added by major browsers themselves, which cannot be used as variable names.
The above is about the content of this article on "how to invoke javascript strict mode". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.