I am using “for...of” in many places in my Javascript code which is a new technology, part of Javascript’s new specification (ECMAScript 2015 (E56) standard). "for...of" is supported in Chrome, Safari and Firefox. Though, IE doesn’t yet support this new feature (See this link). What would be my best approach to support these 4 main browsers - do I revert to "for in/for loop" or do I use browser-sniffing code or is there a better hack to be able to both use some hybrid "for...of" and "for...in"? This is not a repeat of this question here: link_here because I am aware that IE does not support "for...of". What I am looking for is if there is a hack to still use it and still support all the 4 browsers I listed?
Asked
Active
Viewed 307 times
0
-
2It's ES6, not E56 :) – Oriol Jun 18 '15 at 22:09
-
You can't polyfill new syntax so the only option to support IE is to use regular for loops. – JJJ Jun 18 '15 at 22:13
-
3You should consider using an ES6 to ES5 compiler like [Babel](https://babeljs.io/). – Cymen Jun 18 '15 at 22:13
2 Answers
1
You can add the Traceur compiler:
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script type="module">
for(var n of ['foo', 'bar'])
alert(n);
</script>
It should work even on IE9.

Oriol
- 274,082
- 63
- 437
- 513
0
If you want back compat support for "for...of" your only choice is a compiler (transpiler). If you look here: https://kangax.github.io/compat-table/es6/ the well known compilers, and the level of support they provide, are well documented.

balrob
- 585
- 6
- 7