What does this reticence mean in the array? [duplicate]
I've seen that ES6 tends to be cleaner, but I can't understand a few things:
1. What exactly is this reticence is doing, in theory?
function getHiddenHTML(el) {
return [...el.childNodes].reduce((txt, node) => {
//results
});
I know it would be something like this in ES5, to work, which made me even more confused, what exactly these functions are doing, I would like a theoretical explanation about it, it was worth:
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 answers
This is spread syntax and is a simple way to convert something iterable into an array. When you use querySelectorAll
for example, it returns something similar to an array but not a real 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 for polyfill being so complex is because there are different types of "iterables" and what you want is to convert into an array.