-2

Here's a sample from Redux.js I am reding on github. Can someone please explain the syntax used here?

var currentListeners = []
var nextListeners = currentListeners

....
someFunc() {

// THIS:
var listeners = currentListeners = nextListeners
for (var i = 0; i < listeners.length; i++) {
    listeners[i]()
}
.....
}

Are the multi-assignment and for statements independent? that would explain it. But what the missing semicolon at the end of the assignment? good practice / bad practice?

Jeremy
  • 1
  • 85
  • 340
  • 366
user3213604
  • 407
  • 1
  • 7
  • 18
  • 5
    Uhm, isn't that just regular old javascript, written by someone that had a broken keyboard *(missing semicolons)* – adeneo Apr 19 '16 at 23:35
  • All 3 are set equal, to nextListeners I believe. – Evan Carslake Apr 19 '16 at 23:36
  • 1
    Strongly recommend you to take a look at [the basics of assignments at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Assignment_2). – Derek 朕會功夫 Apr 19 '16 at 23:40
  • 2
    The assignment operator evaluates to whatever has been assigned. As a side effect, it updates the value of the LHS. So a = b = c assigns the value of c into b, evaluates to c, and then assigns the value of c into a, and evaluates to c. – Purag Apr 19 '16 at 23:40
  • Possible duplicate of [Is setting multiple variables in 1 line valid in javascript? (var x=y='value';)](http://stackoverflow.com/questions/7581439/is-setting-multiple-variables-in-1-line-valid-in-javascript-var-x-y-value) – Jeremy Apr 19 '16 at 23:51

2 Answers2

3

The assignment operator evaluates to whatever has been assigned (effectively the right operand). As a side effect, it updates the value of the left operand. So a = b = c assigns the value of c into b, evaluates to c, and then assigns the value of c into a, and evaluates to c.

Assignment is right-associative -- it groups from right to left.

Further, semicolons between statements are [semi-]optional if each statement is on its own line (there is some detail missing here that's covered in the link).

Finally, one interesting thing going on here is assignment to a variable declared one scope up. Functions in Javascript are one way to declare a new scope. Note that if the function is just being defined in your snippet, it won't have an effect on the value of currentListeners until it's called.

Community
  • 1
  • 1
Purag
  • 16,941
  • 4
  • 54
  • 75
0
var listeners = currentListeners = nextListeners

is the same thing as doing this:

var currentListeners = nextListeners;
var listeners = currentListeners;

Hope this helps

helfi
  • 125
  • 3
  • 10
  • 1
    Technically no, `currentListeners` is not declared in the current scope. – Derek 朕會功夫 Apr 19 '16 at 23:41
  • thank you; but the multi-assign isn't an issue. the lack of semicolon between assign and for statements, made me wonder whether some ES6/7 feature is at work. From a distance, the lack of semicolons clearly explains it and now I am embarassed. – user3213604 Apr 19 '16 at 23:42
  • @user3213604 The lack of semicolons makes no difference to the behahviour of this code. – Jeremy Apr 19 '16 at 23:43
  • just the readability suffers with lack of semicolons that's all. with Go or Python, the syntax is visually unambiguous enough so that the lack of semicolons do not matter. With JS, mixed with HTML, JSX and first class functions, anonymous functions - 'reading code' becomes difficult with lack of semicolons.. – user3213604 Apr 19 '16 at 23:48