Actually Ma Jerez's answer makes a very important point here. also this answer refers to this similar question:
this question involves a few things:
- hoist: variables are hoisted before block code execution;
=
assignment order: it goes from right to left;
- global context: in non-strict mode, when a variable isn't defined, it goes to the global context; but will throw in 'use strict' mode;
example1:
;(function Anonymous(){
var a = b = {};
console.log(a==b); //true
})();
a
was hoisted in the Anonymous
execution scope.
b
is going to be assigned as {}
, but because b
is not defined, b
is assigned to the global context window
, then window.b
is assigned {}
; then window.b = {}
return
s {}
.
local variable a
is assigned as {}.
Therefore, a few interesting things happen here: the local variable a
and the global variable b
both point to the same object {}
, so they are ==
and ===
; remember that {}=={}
gives false
otherwise.
Note: if in strict mode:
;(function Anonymous(){
'use strict'
var a = b = {}; //Uncaught ReferenceError: b is not defined
})();
this consecutive assignment won't work the same way...