Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the continuous assignment Operation of Javascript

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand the continuous assignment operation of Javascript. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The editor will share the author's experience in writing Javascript, which is about some skills of continuous assignment operation. It must be of great inspiration and help to everyone in the development of Web.

I. introduction

Var a = {n undefined 1}; A. x = a = {n v v 2}; alert (a.x); / /-- > undefined

This is Cai Cai found this way of writing when looking at the jQuery source code. The second sentence a. X = a = {nbank 2} above is a continuous assignment expression. What happened to this continuous assignment expression inside the engine? How do you explain it?

Second, conjecture

Conjecture 1: from left to right, A.X is first assigned to {NRV 2}, but then an is assigned to {NRV 2}, that is, an is rewritten, and the new a has no x attribute, so it is undefined. The steps are as follows

A. X = {nbank 2}; a = {n lug 2}

The result of this explanation is consistent with the actual operation result, which seems to be correct. Note that a.x has been assigned in conjecture 1.

Conjecture 2: from right to left, an is first assigned to {nburet 2}, and A.X finds that an is rewritten (before an is {aRom 1}), and the a.x = {nRO 2} engine restricts a.x assignment and ignores it. The steps are as follows:

A = {nappl2}; a.x is not assigned {npur2}

It is equivalent to a.x = (a = {nlv 2}), that is, the * step is performed, which can also be interpreted as a.x as undefined. Note that A.X has not been assigned at all in guess 2.

III. Proof

The above two conjectures are believed to be shared by most people, and the discussion in the group is regarded as conjecture 1, which I think is conjecture 2. Actually, it's all wrong. I ignored the relationship of citation. As follows, add a variable b to point to a.

Js code

Var a = {var 1}; var b = a; / / hold a to check back A. x = a = {n alert 2}; alert (a.x); / /-- > undefined alert (b.x); / /-- > [object Object]

It is found that a.x is still undefined, but miraculously, b.x has not been assigned (for example, b.x = {n _ 2}), but has become [object Object]. B points to a ({nregious 1}), and only when a.x = {n 2} is executed does it mean that b has an x attribute. The actual execution process: from right to left, an is first assigned as {nvirtual 2}, and then a.x is assigned as {nvirtual 2}.

A = {nappl2}; a.x = {npur2}

Equivalent to

A. X = (a = {nvvl 2})

The difference from conjecture 2 is that a.x is assigned, but not in conjecture 2. The most important difference is that the an in step a = {nappl2} points to the new object {nburet 2}, and the an in the second step a.x = {nburet 2} is {aburete 1}. That is, in this joint statement.

Js code

A. X = (a = {nvvl 2})

The an in a.x points to {nburet 1}, and a points to {nburet 2}. The figure below is as follows

Four: solve the doubt.

After writing this article, maybe some people still feel dizzy after reading it. Because the text description in it is really roundabout. At first, when I understood this joint assignment statement,

Js code

Var a = {NVOR 1}; a. X = a = {NRV 2}

It is thought that the engine will limit the rewriting of a.x (after an is rewritten), but this is not the case. The object you point to is different. The engine also has no restrictions on the rewriting of a.x = {nvirtual 2}.

Thank you to all the people who participated in the discussion: Cai Cai, pig large intestine, dull, elegant ru. This question was first raised by Cai Cai. Yaru is so devoted to every discussion in the rookie gray group, even if it is a topic raised by others.

Five: end

Well, it ends with another continuous assignment problem. After the fun is executed, the variable b here overflows outside the fun to become a global variable. Did you think of it?

Js code

Function fun () {var a = b = 5;} fun (); alert (typeof a); / /-- > undefined alert (typeof b); / /-- > number's continuous assignment operation on how to understand Javascript is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report