//function foo()
function foo() {
return [1,2,3];
}
//function bar()
function bar() {
return {
x: 4,
y: 5,
z: 6
};
}
//assign array and object's inner values to global variables
var tmp = foo(),
a = tmp[0], b = tmp[1], c = tmp[2];
console.log( a, b, c ); //1 2 3
var tmp = bar(),
x = tmp.x, y = tmp.y, z = tmp.z;
console.log( x, y, z ); // 4 5 6
//same using destructuring
var[a,b,c]=foo();
var {x:x,y:y,z:z}=bar();
In the above snippet, var[a,b,c]=foo();
equals to :
var a = foo()[0];
var b = foo()[1];
var c = foo()[2];
var {x:x,y:y,z:z}=bar();
equals to:
var x = bar().x;
var y = bar().y;
var z = bar().z;
right?
Also, one of the potential purpose of doing this is to save that multi lines of code just to declare variables right?
and inside var {x:x,y:y,z:z}=bar();
the x,y,z that is left to the :
are the global variables we just declared right? the ones that are on the right of : are the internal values of the object.
Am I understanding this correctly?
Thanks .