Detailed description of the strict Javascript Mode
Source: Internet
Author: User
1. In addition to the normal running mode, ECMAscript5 adds the second running mode: & quot; strict mode & quot; (strictmode ). As the name suggests, this mode allows Javascript to run under more stringent conditions. The purpose of setting up the & quot; strict mode & quot; is to:-eliminate Java... sy 1. In addition to the normal running mode, ECMAscript 5 adds the second running mode: strict mode ). As the name suggests, this mode allows Javascript to run under more stringent conditions. The purpose of setting up the "strict mode" is to eliminate unreasonable and non-rigorous Javascript syntax and reduce some weird behaviors.-eliminate some Insecure code running, ensure the security of code running.-improve compiler efficiency and speed;-pave the way for future new Javascript versions. "Strict mode" reflects the more reasonable, safer, and more rigorous development direction of Javascript. Mainstream browsers, including IE 10, have supported it, many large projects have begun to embrace it. On the other hand, the same code may have different running results in "strict mode"; some statements that can be run in "Normal Mode, it cannot run in "strict mode. Understanding this content helps you understand Javascript in a more detailed and in-depth manner and turn you into a better programmer. This article will introduce "strict mode" in detail. 2. Enter the flag to enter the "strict mode". It is the following line of statement: "use strict"; the browser of the old version treats it as a regular string and ignores it. 3. There are two methods for calling "strict mode", which are suitable for different scenarios. 3.1 place "use strict" in the first line of the script file for the entire script file, and the entire script will run in "strict mode. If this line of statements is not in the first line, it is invalid. The entire script runs in "normal mode. If code files in different modes are merged into one file, pay special attention to this. Script "use strict"; console. log ("this is a strict mode. "); Script, script console. log (" this is the normal mode. "); The Code in script indicates that a webpage contains two Javascript codes in sequence. The tag of the previous script is in strict mode, and the tag of the last script is not. 3.2 If "use strict" is placed on the first line of the function body for a single function, the entire function runs in "strict mode. Function strict () {"use strict"; return "this is a strict mode. ";} Function notStrict () {return" this is the normal mode. ";} 3.3 The workarounds of the script file because the first call method is not conducive to file merging, it is better to use the second method, place the entire script file in an anonymous function that is executed immediately. (Function () {"use strict"; // some code here}) (); 4. Change the syntax and behavior of the strict mode against Javascript, all made some changes. 4.1 The global variables are explicitly declared in normal mode. If a variable is not declared, the value is assigned. The default value is the global variable. This is not allowed in strict mode. The global variables must be explicitly declared. "Use strict"; v = 1; // error, v is not declared for (I = 0; I}. Therefore, in strict mode, variables must be declared with the var command, and then use it. 4.2 A feature of static binding to Javascript is to allow "dynamic binding", that is, the object to which certain attributes and Methods belong, which is not determined during compilation, it is determined at runtime. The strict mode imposes some limitations on dynamic binding. In some cases, only static binding is allowed. That is to say, the object to which the attribute and method belong is determined at the compilation stage. This will help improve compilation efficiency and make the code easier to read and avoid accidents. Specifically, it involves the following aspects. (1) It is forbidden to use the with statement because the with statement cannot be determined during compilation, and the object to which the attribute belongs. "Use strict"; var v = 1; with (o) {// syntax error v = 2;} (2) create eval scope in normal mode, javascript has two variable scopes: Global and function scopes. Strict mode creates the third scope: eval scope. In normal mode, the scope of an eval statement depends on whether it is in the global scope or function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables. The variables generated by the eval statement can only be used inside eval. "Use strict"; var x = 2; console.info (eval ("var x = 5; x"); // 5 lele.info (x ); // 2 4.3 enhanced security measure (1) disable this keyword to point to the global object function f () {return! This;} // returns false because "this" points to a 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, if you forget to add new when using the constructor, this will not point to the global object, but will report an error. Function f () {"use strict"; this. a = 1 ;}; f (); // an error is returned. this is not defined. (2) traversal of the Call Stack function f1 () {"use strict"; f1.caller; // error f1.arguments; // error} f1 (); 4.4 forbidden to delete variables in strict mode. Only the property of an object whose retriable is set to true can be deleted. "Use strict"; var x; delete x; // syntax error var o = Object. create (null, 'x', {value: 1, retriable: true}); delete o. x; // deletion successful 4.5 explicit error: In normal mode, the read-only attribute of an object is assigned a value. No error is reported, but the operation fails silently. In strict mode, an error is reported. "Use strict"; var o ={}; Object. defineProperty (o, "v", {value: 1, writable: false}); o. v = 2; // in strict error reporting mode, if you assign values to an attribute read using the getter method, an error is returned. "Use strict"; var o = {get v () {return 1 ;}}; o. v = 2; // in strict error reporting mode, an error is returned when a new attribute is added to an object that is not extended. "Use strict"; var o ={}; Object. preventExtensions (o); o. v = 1; // an error is returned when an attribute that cannot be deleted is deleted in strict mode. "Use strict"; delete Object. prototype; // error 4.6 duplicate name error some syntax errors are added in strict mode. (1) The object cannot have duplicate attributes. In normal mode, if the object has multiple duplicate attributes, the attribute assigned at the end will overwrite the previous values. In strict mode, this is a syntax error. "Use strict"; var o = {p: 1, p: 2}; // syntax error (2) the function cannot have duplicate parameters in normal mode, if the function has multiple parameters with duplicate names, you can use arguments [I] to read them. In strict mode, this is a syntax error. "Use strict"; function f (a, a, B) {// syntax error return;} 4.7 disable the first integer in normal mode. If it is 0, it indicates the octal number, for example, 0100 is equal to 64 in decimal format. This notation is not allowed in strict mode. If the first integer is 0, an error is returned. "Use strict"; var n = 0100; // syntax error 4.8 arguments object restriction arguments is the parameter object of the function, and its use is restricted in strict mode. (1) The arguments value cannot be "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 traces parameter changes function f (a) {a = 2; return [, arguments [0];} f (1); // Normal Mode: [2, 2] function f (a) {"use strict"; a = 2; return [, arguments [0];} f (1); // The strict mode is [2, 1] (3) disabling arguments. callee means that you cannot call yourself within an anonymous function. "Use strict"; var f = function () {return arguments. callee ;}; f (); // error 4.9 The function must declare that "block-level scope" will be introduced in future new Javascript versions at the top layer ". To comply with the new version, the strict mode only allows you to declare a function at the top level of the global scope or function scope. That is to say, you cannot declare a function in a non-function code block. "Use strict"; if (true) {function f () {}// syntax error} for (var I = 0; I function f2 () {} // syntax error} 4.10 reserved words: In order to transition to a new version of Javascript, some reserved words are added in strict mode: implements, interface, let, package, private, protected, public, static, yield. If you use these words as variables, an error is reported. Function package (protected) {// syntax error "use strict"; var implements; // syntax error} In addition, the fifth edition of ECMAscript itself also specifies other reserved words: class, enum, export, extends, import, super. They are also unavailable.
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.