Hi to all,
considering this piece of code based on one of ES6 exercises, i cant explain myself a specific behaviour of of objects as functions with high order functions.
INPUT : result Object
OUTPUT : [ ‘no-var’, ‘var-on-top’, ‘linebreak’ ]
In makeList function , i have created two objects ( firstArray , secondArray ).
The first one return the correct object set, while the second is returned as a function.
If i modify the second function into
let secondArray = ;
let myFunc = (…arr) => { // [ ‘no-var’, ‘var-on-top’, ‘linebreak’ ]
return arr.reduce((a) => a);
};
secondArray = myFunc(arr);
It works, so it comes the question.
What is the difference between the two functions ? Seems that the one with the spread operator needs to be called explicitly.
Thanks
const result = {
success: [“max-length”, “no-amd”, “prefer-arrow-functions”],
failure: [“no-var”, “var-on-top”, “linebreak”],
skipped: [“id-blacklist”, “no-dup-keys”]
};function makeList(arr) {
“use strict”;let firstArray = arr.map((i) => { return i; }); let secondArray = (...arr) => { return arr.reduce((a) => a); }; return secondArray;
}
const resultArray = makeList(result.failure);
console.log(‘resultArray :’, resultArray);