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.
Differences in JavaScript strict mode
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
