What order does the following statement run? Does the runtime execute it from right to left?
length = i = test = output = null;
Does the runtime execute it from right to left?
Yes.
foo = {
set a() {
console.log('a');
},
set b() {
console.log('b');
},
set c() {
console.log('c');
},
set d() {
console.log('d');
}
};
foo.a = foo.b = foo.c = foo.d = 'bar';
produces:
d
c
b
a
in the console. This order is necessary because each assignment relies on the return value of the previous assignment:
foo.a = foo.b = foo.c = foo.d = 'bar';
is equivalent to:
foo.a = (foo.b = (foo.c = (foo.d = 'bar')));
However it is not equivalent to:
foo.d = 'bar';
foo.c = foo.d;
foo.b = foo.c;
foo.a = foo.b;
The return value of a = b
is b
. This is especially important to remember should you choose to implement accessors and mutators.
What this means is that my previous example is equivalent to:
foo.d = 'bar';
foo.c = 'bar';
foo.b = 'bar';
foo.a = 'bar';