for-of-loop

The meaning of “'x' is not a function or its return value is not iterable” error

自作多情 提交于 2021-01-20 04:17:39
问题 I accidentally witnessed that this causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable It appears that any other non-iterable value (including a function) causes another error: for (let val of function () { throw 'never called' }) { /*...*/ } TypeError: (intermediate value) is not iterable As the reference states, the error is specific to Chrome: TypeError: 'x' is not a function or its

The meaning of “'x' is not a function or its return value is not iterable” error

被刻印的时光 ゝ 提交于 2021-01-20 04:16:00
问题 I accidentally witnessed that this causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable It appears that any other non-iterable value (including a function) causes another error: for (let val of function () { throw 'never called' }) { /*...*/ } TypeError: (intermediate value) is not iterable As the reference states, the error is specific to Chrome: TypeError: 'x' is not a function or its

for…of loop. Should I use const or let?

徘徊边缘 提交于 2020-08-24 06:20:26
问题 When using a for of loop, both of these are allowed and work: const numbers = [1,2,3]; // works for(let number of numbers) { console.log(number); } // also works for(const number of numbers) { console.log(number); } I always use const since I annot fanthom changing the number variable in any context, but when I see a for...of loop in other people's code, it often uses let . Maybe there's a drawback to const that I didn't see? Browser bugs? Why use const and when to use let in for...of loops?

Is there no difference between let and const inside for of loop in Javascript in terms of declaration?

[亡魂溺海] 提交于 2020-04-14 08:29:49
问题 I recently came across this code: for (const temp of [1,2]) { // do something } I thought that it'd be better to use let declaration for temp because this way the variable would be declared only once. However, I also ran this example as well as the version with let through babel and this is what I see: for (const p of [1,2]) { } for (let s of [1,2]) { } became: for (var _i = 0, _arr = [1, 2]; _i < _arr.length; _i++) { var p = _arr[_i]; } for (var _i2 = 0, _arr2 = [1, 2]; _i2 < _arr2.length;

Is there no difference between let and const inside for of loop in Javascript in terms of declaration?

99封情书 提交于 2020-04-14 08:28:08
问题 I recently came across this code: for (const temp of [1,2]) { // do something } I thought that it'd be better to use let declaration for temp because this way the variable would be declared only once. However, I also ran this example as well as the version with let through babel and this is what I see: for (const p of [1,2]) { } for (let s of [1,2]) { } became: for (var _i = 0, _arr = [1, 2]; _i < _arr.length; _i++) { var p = _arr[_i]; } for (var _i2 = 0, _arr2 = [1, 2]; _i2 < _arr2.length;

Type 'HTMLCollectionOf<HTMLCanvasElement>' must have a '[Symbol.iterator]()' method that returns an iterator

这一生的挚爱 提交于 2020-03-04 06:42:28
问题 I am building an Array with const myCanvas = documen.getElementsByTagName('canvas') that it's actually working. It returns me something like this: images: [ 0: canvas, 1: canvas, 2: canvas ] This is for a Typescript project, I want to iterate this Array and transform each image in order to log it. Like this: for (const image of myCanvas) { console.log(canvas.toDataURL()); } (I am not using foreach because it doesn't works with HTMLCollectionOf type) I need to iterate the HTMLCollection that

Javascript simple for loop versus for…of performances

↘锁芯ラ 提交于 2020-02-28 07:54:47
问题 I have seen that since ECMA 6 we can use for...of instead of the traditionnal for loop: for( let i = 0 ; i < arr.length ; i++ ) { var elm = arr[i]; // do stuff } VS for( let elm of arr ) { // do stuff } Has you see the second one is lot more readable, simple and maintainable! I just wonder how performant the second syntax is as I need to use it a lot in a render loop (60 times per seconds) for a game. Have you got a clue ? 回答1: The first one (the standard for-loop) performs much better

Javascript for…of doesn't work in Safari

雨燕双飞 提交于 2020-01-13 12:07:13
问题 Currently I am trying to build a simple sidenavigation that appears/disappears whenever one of the "toggleSidenav" buttons is clicked (there are multiple). It seemed to work fine when testing with Firefox and Chrome but today when I tried to open my page with Safari (desktop and mobile version) the buttons didn't do anything. The problem seems to be the for-of-loop I used but checking the reference of the for...of, Safari should support it. My code: for (var btn of document

Javascript for…of doesn't work in Safari

落花浮王杯 提交于 2020-01-13 12:06:57
问题 Currently I am trying to build a simple sidenavigation that appears/disappears whenever one of the "toggleSidenav" buttons is clicked (there are multiple). It seemed to work fine when testing with Firefox and Chrome but today when I tried to open my page with Safari (desktop and mobile version) the buttons didn't do anything. The problem seems to be the for-of-loop I used but checking the reference of the for...of, Safari should support it. My code: for (var btn of document

Javascript for…of doesn't work in Safari

隐身守侯 提交于 2020-01-13 12:06:50
问题 Currently I am trying to build a simple sidenavigation that appears/disappears whenever one of the "toggleSidenav" buttons is clicked (there are multiple). It seemed to work fine when testing with Firefox and Chrome but today when I tried to open my page with Safari (desktop and mobile version) the buttons didn't do anything. The problem seems to be the for-of-loop I used but checking the reference of the for...of, Safari should support it. My code: for (var btn of document