How to use JS strict mode in your project
This time I will show you how to use JS strict mode in the project. What are the precautions when using JS strict mode in the project. The following is a practical case, let's take a look.
What is strict mode?
A mode that makes JS coding more standardized and eliminates some unreasonable Javascript syntax , loose parts, reduce some weird behaviors
How to use?
Just add the following string, this This syntax can be backward compatible. If it is a javascript engine that does not support strict mode, it will be directly regarded as an unassigned string literal and will be ignored directly. The supported engine will enable strict mode
'use strict'
Explanation:
1. If used in the global scope, the entire js script will turn on this mode
2. If it is only used inside the function, then it is only enabled inside the function
function doSomething(){ 'use strict' // 其他代码 }
Variable
1. What is in strict mode? There are restrictions on when and how to create variables. First of all, accidental creation of global variables is not allowed. In non-strict mode, you can create global variables as follows, but in strict mode, this is not possible
// Undeclared variables
// Non-strict mode: create global variables
// Strict mode: throw referenceError error
2. In strict mode, for variables There are also restrictions on names. In particular, you cannot use reserved words such as implements, interface, let, package, private, etc. as variable names. If you use these variables to name them, you will get a syntax error
Object
Operation of objects in strict mode is more likely to cause errors than in non-strict mode. The following situations will cause syntax errors
1. Assigning a value to a read-only property will throw a TypeError
2. Using the delete operator on a non-configurable property will throw a TypeError
3. Adding a property to a non-extensible object will throw a TypeError
Function
In strict mode, it is required that the parameters of the named function must be unique
// 重命名参数的 // 非严格模式:没有错误,只能访问第二个参数,如果要访问第一个参数,就必须通过arguments // 严格模式语法错误 function sum(num,num){ //do something }
The behavior of the arguments object is also different in strict mode. In strict mode, modifying the value of the named parameter will also be reflected in the arguments object, but in strict mode these two values are completely independent
function showValue(value){ value = 'foo' console.log(value) console.log(arguments[0]) // 非严格模式 : 'foo' 严格模式 :'hello' } showValu('hello')
Function
## In strict mode, the parameters of the function must be unique// 重名的参数 // 非严格模式中,没有错误,只能访问第一个参数 // 严格模式 :抛出错误 Uncaught SyntaxError: Duplicate parameter name not allowed in this context function sum(num,num){ 'use strict' // do something }
arguments object
In non-strict mode, modifying the value of the named parameter will also be reflected in the arguments object, but in strict mode, the two values are completely independent// 修改命名参数的值 // 非严格模式:修改会反应到arguments中 // 严格模式中不会反应到arguments中 function sum(num,num2){ 'use strict' num=3 console.log(arguments[0],num2)// 严格模式下输出为1,2 非严格模式下输出为3,2 } sum(1,2)
eval()
The biggest change of the eval function is that it no longer creates variables or functions in the containing context:// 使用eval函数创建变量 // 非严格模式中:弹出框弹窗 20 // 严格模式中:调用alert(x)时报错 function doSomething(){ eval('var x=20') alert(x) }
'use strict' var result=eval('x=1,y=13;x+y') alert(result)
Suppress this
In non-strict modeUse the apply( of function ) or call() method, null and undefined values will be converted to global objects, and in strict mode, the this value of the function is always the specified value, no matter what value is specified:
// 访问属性 // 非严格模式:访问全局实行 // 严格模式:抛出错误,因为this的值是null var color = "red"; function displayColor() { alert(this.color); } displayColor(null);
Other changes
在严格模式中with语句被抛弃掉了,在非严格模式中with语句能够改变解析标识符的路径,但在严格模式下,with语句被简化掉了,因此,在严格模式下使用with语句是导致语法错误
// with语句 // 非严格模式:允许 // 严格模式:抛出语法错误 with (location) { console.log(href); }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读
The above is the detailed content of How to use JS strict mode in your project. 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

What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

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

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

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
