Classic case of continuous assignment in js
Today I encountered a classic case of continuous assignment. The answers given by netizens are also varied, which seems a bit cumbersome. Let me share my opinions.
In order to take care of beginners, I used the simplest, easy-to-understand and even somewhat verbose words to explain my understanding of this case. It is actually not difficult.
The following is this classic case:
<span style="font-size: 14px"><span style="color: #0000ff">var</span> a = {n: 1<span style="color: #000000">};</span><span style="color: #0000ff">var</span> b =<span style="color: #000000"> a; a.x </span>= a = {n: 2<span style="color: #000000">};<br>console.log(a);<br>console.log(b); console.log(a.x); console.log(b.x); <br></span></span>
Let’s take a look at ordinary continuous Assignment, that is: the type of variable assignment is the data type value
var a=3;var b=a=5; console.log(a); console.log(b);
Generally speaking, the direction of equal sign assignment is from right to left, then the above code Equivalent to the following code, then we will use the following code to explain the above code:
<span style="font-size: 14px"> <span style="color: #0000ff">var</span> a=3<span style="color: #000000">; //全局变量a被赋值为3</span><span style="color: #0000ff">var</span> a=5<span style="color: #000000">; //此时a被重新赋值为5</span><span style="color: #0000ff">var</span> b=<span style="color: #000000">a; //将a的值赋给全局变量b<br></span></span>
<span style="font-size: 14px"> console.log(a);//a最终的值就是第二次被赋的值:5<br> console.log(b);//按照代码执行顺序b的值也是:5</span>
The above small case It is used to attract new ideas. Now let’s analyze this classic case:
<span style="font-size: 14px"><span style="color: #0000ff">var</span> a = {n: 1<span style="color: #000000">};//a第一次被赋值,是一个引用类型值,请记得变量赋值为引用类型值的时候,通过变量改变这个对象的时候,对象本身也发生了变化</span><span style="color: #0000ff">var</span> b =<span style="color: #000000"> a;//b被赋值为a,因此b就是对象{n:1}<br>a.x </span>= a = {n: 2</span><span style="color: #000000"><span style="font-size: 14px">};</span><br><span style="font-size: 14px">//这个赋值与之前的简单案例有所不同,a.x指的是给a添加一个x属性,在js的运算中“.”和"="运算符同时出现,会先执行"."运算</span><br><span style="font-size: 14px">//因此,赋值顺序被改变了,是先给a.x赋值,再给a赋值</span><br><span style="font-size: 14px">//就是先执行:a.x={n:2},注意这里a并未改变,是给a的x属性赋值为{n:2},a还是{n:1}<br>//再回到我代码中的第一句话,这个赋值行为,改变了{n:1}这个对象,即给它增加了名为x的属性</span><br><span style="font-size: 14px">//再执行a={n:2},这是变量a不再是对象{n:1},而被重新赋值为一个新的对象{n:2}; console.log(a);//自然此时a是对象{n:2}</span><br><span style="font-size: 14px">console.log(b);//a的二次赋值,并没有影响b,b还是对象{n:1},但是由于a在重新赋值之前,给{n:1}这个对象,增加了一个x属性,因此,这时的b已经有了x属性 console.log(a.x);//{n:2}对象没有x属性,所以结果是undefined console.log(b.x);//综上所述,这个结果是{n:2} </span> </span>
I don’t know if you understand?
The above is the detailed content of Classic case of continuous assignment in js. 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











You can easily copy and paste text and files between VMware virtual machines (VMs) and physical systems. This capability allows you to easily transfer images, formatted and unformatted text, and even email attachments between virtual machines and host systems. This article will show you how to enable this feature and demonstrate methods for copying data, files, and folders. How to Enable Copy/Paste in VMware VMware provides three different ways to copy data, files or folders from a virtual machine to a physical computer and vice versa, as explained below: Copy and Paste Elements Drag and Drop Feature Folder Sharing 1 ] Enable copy-paste using VMware Tools You can use the keyboard if your VMWare installation and guest operating system meet the requirements

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

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

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.

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
