Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite0
If we assume to just 'index' variable, it would also work! then why they use BackTick(`${}`) method? If you will to answer it?
const arr = ['0', '1', '4', 'a', '9', 'c', '16']; const my_obj = { [Symbol.iterator] : function* (){ for(let index of arr) { yield `${index}`; // if we assume to just 'index' variable then it would also work! then why they use BackTick method? } } } const all = [...my_obj] .map(i => parseInt(i, 10)) .map(Math.sqrt) .filter((i) => i < 5 ) .reduce((i, d) => i + d); console.log(all)
4 Réponses
+ 4
Perhaps it was intentional to use string interpolation, to yield a string form of each element from <arr> rather than the actual element themselves.
Not sure the reason why, *probably* to prevent accidental modification to <arr> elements.
I guess you knew that - when an element of <arr> was an object - yielding the element means to yield it's reference.
And that, would make (intentional and/or unintentional) modification to the said element in <all> array, to also reflect in <arr> array (a speculative opinion - tested).
Such thing would not happen if the string form of the element was yielded instead of the actual element. Because in that case, none of the elements in <all> array refer to an element in <arr> array.
(Edited)
+ 1
Roshaan thanks bro for your fabulous reply .. I think, I should go with the default code as on ES6 modules mentioned on.. anyway thanks again..
+ 1
Ipang thanks.. yeah.. might be the reason as well!
0
Mirielle yeah! exactly, what I'm asking for!