for...of - JavaScript 关键字 Internet Explorer 解决方法

for...of - JavaScript keyword Internet Explorer work-around

我在 Javascript 代码中的许多地方都使用了“for...of”,这是一项新技术,是 Javascript 新规范(ECMAScript 2015 (E56) 标准)的一部分). "for...of" 在 Chrome、Safari 和 Firefox 中受支持。虽然,IE 还不支持这个新功能(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 因为我知道 IE 不支持 "for...of"。我正在寻找的是是否有 hack 仍然可以使用它并且仍然支持我列出的所有 4 个浏览器?

您可以添加 Traceur 编译器:

<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>

它甚至应该在 IE9 上工作。

如果你想要 "for...of" 的反向兼容支持,你唯一的选择是编译器(transpiler)。如果您在这里查看:https://kangax.github.io/compat-table/es6/ 众所周知的编译器及其提供的支持级别都有详细记录。