JS strict mode example explanation
This article mainly organizes relevant knowledge points and code examples in JS strict mode for everyone. If you are interested, follow the editor to learn together. I hope it can help everyone.
The so-called strict mode is actually a string "use strict" that will not be assigned to any variable
If this prompt is given in the global scope, then the entire The script will be in strict mode. You can also turn on strict mode only in the function
1. There are no global variables in strict mode
a="test"
strict An error will be reported in mode, but normal in non-strict mode
2. Delete variables
var There are three declaration situations
-
var Global variables declared in
var Local variables declared in
Global variables declared in eval()
The first and second cases cannot be deleted using delete.
First of all, although the global object declared in the first case is a property of windows, this property has configurable=false and therefore cannot be deleted.
The second case is local Variables are even worse. You don’t even know who the attached object is. How to delete them
The third case eval() will be discussed later.
Back to the topic. That is to say, in non-strict mode, you can delete variables, but it will fail and return false.
Deleting variables in strict mode will report an error.
3. Object
Under the following circumstances, an error will be reported when operating the object
Assigning a value to a read-only attribute will Error reporting
Using delete for non-configurable properties will report an error
Adding properties to non-extensible objects will report an error
When using object literals, property names must be unique. For example,
var person={ name:"1", name:"2" }
will default to the second value in non-strict mode, and an error will be reported in strict mode.
4. Function
Strict mode requires that function parameter names must be unique
function(n,n){ // todo }
In strict mode, this function declaration will not report an error. Only the second parameter can be accessed through the parameter name, and the first parameter must be accessed through arguments.
arguments are also different in the two modes
In non-strict mode, modifying the value of the named parameter will be reflected in the arguments object. In strict mode, both The values are independent.
Eliminated arguments.callee (referencing the function itself) and arguments.caller (referencing the calling function).
In strict mode, function names cannot use js reserved words
**In strict mode, functions can only be declared at the top level of the script and inside the function, in the if statement Declaring a function results in a syntax error. **
if(true){ function(){ // 严格模式下报错 } }
5.this
Use function apply(), call(() in non-strict mode ), the null or undefined value passed in will be converted into a global object. In strict mode, the this of a function is always the specified value, no matter what value is specified.
var a="1"; function test(){ console.log(this.a) } test.call(null) 在非严格模式下会输出1,严格模式下this就指代的就是null,null没有a属性,就会报错。
Related recommendations:
A brief introduction to the parsing order and scope of JS and strict mode
JS parsing order scope strict mode parsing
Detailed explanation of the pointing problem of this in JavaScript strict mode
The above is the detailed content of JS strict mode example explanation. 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

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

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
