How/why to chain variables with operator = (equality)?

Asked

Viewed 148 times

8

Sometimes I come across these statements chained in source some libraries and are usually many chained statements. For example:

var foo = foo2 = foo3 = 'foovalue';

However I could never understand the meaning of this and much less a practical use.

Once I found it convenient to make a shortcut to a statement:

var e = window.counter = 0;

Just so I don’t have to repeat myself window.counter on my conditions. It even worked only that in the course of the script, using this shortcut did not update the value of window.counter.

Finally, I am seeking a light on this practice.

  • 1

    Wow man, if you have 1 go inside another... It’s the fastest use that comes to mind.

  • 2

    This: var foo = foo2 = foo3 = 'foovalue'; only creates the variable foo in the current lexical scope (i.e. function), it is good that you have already declared the variables foo2 and foo3 before.

1 answer

6


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.

  • now I understood why window.counter was not updated. Actually it was not a shortcut, rs. There is another more syntactic possibility to do the same thing?

  • 1

    @ropbla9 A shortcut, so that one variable when updated modifies another? No, what I do is an access function: function e() { return window.counter; } and when I need to use: e(). But this would be an interesting feature to have...

  • fantastic, brave!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.