Skip to content Skip to sidebar Skip to footer

Javascript Multiple Asignment Re-evaluation Or Result Passing?

The multiple assignment (or is it called chaining?) that I'm talking about is assignment such as: a = b = c = 2; ...after which a, b, and c are all equal to 2; My question of opti

Solution 1:

This:

dom1.style.height = dom2.style.top = maxLogoHeight - scrollTop;

… is not quite the same as this:

dom2.style.top = maxLogoHeight - scrollTop;dom1.style.height = dom2.style.top;

Instead, the right-hand operand maxLogoHeight - scrollTop is assigned to each of dom1.style.height and dom2.style.top.

We can see this in the following example:

Snippet

var d= document.getElementById('D');

s = d.style.width= 'abc';
console.log(s);             //'abc'

d.style.width= 'abc';
s= d.style.width;
console.log(s);             //null string
#D {height: 100px; width: 100px; background: red;}
<divid="D"></div>

abc is an invalid width, and it is therefore discarded by d.style.width. However, you'll see that s is assigned abc in the first console.log(), and it's assigned the null string in the second console.log().


The following example may be more intuitive:

const x = 3;
var y = x = 6;                   // x cannot change.  So is y 3 or 6?document.body.innerHTML= x + y;  // -> 9 ... y must be 6!

Post a Comment for "Javascript Multiple Asignment Re-evaluation Or Result Passing?"