Something = Something = Something ... What?

I am reading some code and I see a comparison that looks like this:

a = b = c

After seeing how a Javascript search for equal or comparison gives correction results, does anyone want to explain what is happening?

EDIT: These are all the objects or properties of the objects that we are talking about here, should have been indicated.

DOUBLE EDIT: this is inside the Object.defineProperties () block.

+4
source share
5 answers

The = operator links from right to left and evaluates the value that was assigned.

So this is:

 a = b = c; 

So this is *:

 b = c; a = c; 

* If you are not dealing with properties.

+3
source

= is an operator. It takes two arguments: a reference to a variable and an expression. It assigns the value of the expression to the variable and returns the assigned value.

As a result, you can link them and equate to this:

 a = (b = c) 

In other words, assign b to c , then also assign a to this.

+4
source

a = b = c is just an abbreviated expression for:

 b = c; a = b; 

if(a = b) will always return true , because it assigns instead of comparison. For comparison, the statement should look like this: if(a == b) .

+4
source

This is not a comparison. This is the assignment of the value of c to variables b and a .

The assignment works from right to left, so c - b is assigned first. Then the return value of this operation is assigned to a .

The return value of the assignment operation is the value that was assigned, so a will get the same value as b .

+3
source

He equates this:

 b = c; a = b; 
+2
source

Source: https://habr.com/ru/post/1435404/


All Articles