简体   繁体   English

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

[英]for…of - JavaScript keyword Internet Explorer work-around

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). 我在JavaScript代码的许多地方都使用了“ for ... of”,这是一项新技术,是Javascript新规范(ECMAScript 2015(E56)标准)的一部分。 "for...of" is supported in Chrome, Safari and Firefox. Chrome,Safari和Firefox支持“ for ... of”。 Though, IE doesn't yet support this new feature ( See this link ). 不过,IE尚不支持此新功能( 请参阅此链接 )。 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"? 支持这4种主要浏览器的最佳方法是什么?我将还原为“ for in / for循环”,还是使用浏览器嗅探代码,或者有更好的技巧可以同时使用某些“混合”。 .of”和“ for ... in”? This is not a repeat of this question here: link_here because I am aware that IE does not support "for...of". 这不是在这里重复此问题: link_here,因为我知道IE不支持“ 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? 我要寻找的是是否仍有黑客可以继续使用它,并且仍然支持我列出的所有4种浏览器?

You can add the Traceur compiler: 您可以添加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> 

It should work even on IE9. 即使在IE9上也应该可以使用。

If you want back compat support for "for...of" your only choice is a compiler (transpiler). 如果要向后兼容支持“ for ... of”,则唯一的选择是编译器(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. 如果您在此处查看: https : //kangax.github.io/compat-table/es6/ ,众所周知的编译器,以及它们提供的支持级别,都有据可查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM