0

Looking at var a=b=1; , I already know that both a and b has the same value.

But my question is :

Does the a gets its value from 1 or from b ?

I made a small test :

/*1*/   (function (){
/*2*/     var j = window.j = function (){ alert('3');};
/*3*/     window.j2 = j;
/*4*/   })();
/*5*/   
/*6*/   window.j(); //3
/*7*/   window.j=null;
/*8*/   window.j2();//3

As you can see line #8 yields 3 so I persume that a is not having the value of b but the value of 1.

Am I right ?

visualize :

(function (){
  var j = window.j = function (){ alert('3');};
      |
      |          ^        ^
      |          |        |            //which one ?
      +----------+--------+  
})();
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript – Mark Walters Sep 26 '13 at 10:30
  • @MarkWalters I already read it. but it doesnt state from where `a` got the value. it talks about scoping which I already know that. – Royi Namir Sep 26 '13 at 10:31
  • You are right, as one of the answers also states in @MarkWalters link, assignment in javascript works from right to left. – John Klakegg Sep 26 '13 at 10:37

4 Answers4

0

Assigment in JavaScript is right associative, so you are correct. In

a = b = c;

a takes the value of b at time of assignment, so if b is later assigned to something else, a retains its value (which happens to be the same as c)

shanet
  • 344
  • 2
  • 7
  • According to this , Im wrong. `a` gets its value from `b` and not from `1` – Royi Namir Sep 26 '13 at 10:41
  • I don't see how this is the case. In your function, you are assigning the value of `j` to be the value of `window.j` (not a *reference* to `window.j`), to which you are assigning a function as the value that alerts "3". Then you assign window.j2 to the value of `j`. Thus when you invoke `window.j2`, it invokes the value of `j`. – shanet Sep 26 '13 at 10:45
0

You are right technically but are confused with reference / value assignment i think. Technically a does get it's value from b but there is no reference to b therefore if your were to do b = null it would have no affect to a, which is what you're seeing in your example.

a inherits the value of b without relying on b still existing or having that same value later on when you refer back to a. The assignment happens right to left so actually reads a = ( b = 1)

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
0

The "=" operator associates to the right so "a=b=1" is equivalent to "a=(b=1)". So 1 is assigned to b first with a result of 1, which is then assigned to a.

Mike Stotts
  • 86
  • 1
  • 4
0

Assignment in javascript works from right to left. So you are getting your value from window.j. Re-setting window.j will not affect the result because Javascript variables always passes by value, exception is array or object.

Example of passing value by ref in JS object:

var obj = { x: 2 };
var anotherObj = obj;
anotherObj.x++;
alert(obj.x); //3 

You can find more information here.

More useful examples available in this answer.

Community
  • 1
  • 1
Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60