If we assume to just 'index' variable, it would also work! then why they use BackTick(`${}`) method? If you will to answer it? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

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)

16th Feb 2023, 8:13 AM
Sony
Sony - avatar
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)
16th Feb 2023, 11:43 AM
Ipang
+ 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..
16th Feb 2023, 9:57 AM
Sony
Sony - avatar
+ 1
Ipang thanks.. yeah.. might be the reason as well!
16th Feb 2023, 1:25 PM
Sony
Sony - avatar
0
Mirielle yeah! exactly, what I'm asking for!
16th Feb 2023, 9:11 AM
Sony
Sony - avatar