This operator serves to assign more than one variable at the same time, no more and no less. It’s hard to think of such a practical use in the abstract, but I would say that if you need to initialize several variables with the same value in a complex algorithm, and each of those variables will evolve independently, that would be an appropriate use:
var inicio = fim = atual = 10;
while ( ... ) {
if ( lista[inicio-1] <= lista[atual] )
inicio--;
if ( lista[fim+1] >= lista[atual] )
fim++;
}
// o intervalo [inicio,fim] está ordenado (em relação ao atual)
Another possibility is when you want to update more than one data structure with the same value (it may be necessary if two or more different libraries are acting on the same object):
x.atual = y.selecionado = z.emFoco = { ... };
etc. The key point here is that they are independent variables, which may in other circumstances have values different from each other, but at a given time it is desirable that they all have the same value. If a single value is required, it is preferable to use a single variable (delegating access to it through supporting functions if necessary).
// Leitura
function e() { return window.counter; }
// Ou leitura-escrita
function e(v) { return v !== undefined ? window.counter = v : window.counter; }
// Uso
var x = e();
e(10);
P.S. The semantics of the chained operator - if this is unclear - is to assign the all the variables to the left of the last =
the value to the right of it.
Wow man, if you have 1 go inside another... It’s the fastest use that comes to mind.
– HiHello
This:
var foo = foo2 = foo3 = 'foovalue';
only creates the variablefoo
in the current lexical scope (i.e. function), it is good that you have already declared the variablesfoo2
andfoo3
before.– acelent