What does this reticence mean in the array?

Asked

Viewed 882 times

1

I saw that ES6 tends to be cleaner, but I can’t understand some things:

1. What exactly is this reticence doing, in theory?

function getHiddenHTML(el) {
  return [...el.childNodes].reduce((txt, node) => {
     //results
});

I know it would be something like this at ES5, to work, which left me even more confused, what exactly these functions are doing, I would like a theoretical explanation about it, thanks:

function _toArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function getHiddenHTML(el) {
      return _toArray(el.childNodes).reduce(function(txt, node){
         //results
    });

1 answer

2


That is spread syntax and is a simple way to convert something eternal into an array. When using querySelectorAll for example, returns something similar to an array but which is not a truth array. That is, it is iterable in the sense that one can use a loop for and has the method forEach but has no other array methods like reduce.

So with this syntax it is possible to "convert" into a genuine array and with all the methods that an array has.

The reason polyfill is so complex is because there are different types of "iterable" and the intention is to convert it into an array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.