ECMAScript 2015, iterative destructive expression

I am currently experimenting with an iterable expression of destructuring, and I wonder why a particular method does not work. Maybe you can help me.

For example, this works:

var x, y, myIterable = [];
myIterable[Symbol.iterator] = function* () {
  var count = 0;
  while(count < 2){
    yield count++;
  }
};
var myArray = Array.from(myIterable);
console.log(([x,y] = myArray) === myArray);
//OUTPUT: true

But if I try it like this, it will return false, can you explain why?

var x, y, myIterable = [];
myIterable[Symbol.iterator] = function* () {
  var count = 0;
  while(count < 2){
    yield count++;
  }
};
var myArray = Array.from(myIterable);
[x, y] = myArray;
console.log([x,y] === myArray);
//OUTPUT: false
+4
source share
2 answers

===compares the objects on the links as myArrayand [x, y]evaluated in another array.

[] === []; // false
{} === {}; // false
+1
source

, === , , , true, , :

[x, y] = myArray 

[x, y], myArray - RHS, LHS.

, :

([x,y] = myArray) === myArray

LHS === myArray, , RHS, true.

+4

Source: https://habr.com/ru/post/1619698/


All Articles