简体   繁体   English

带有babel es2015预设的webpack是否会解决IE 11上的关闭问题?

[英]Will webpack with babel es2015 preset fix closure issues on IE 11?

If I write a for loop using the let keyword to declare my loop variable, and I pass that variable to a 如果我使用let关键字编写for循环来声明我的循环变量,然后将该变量传递给

 for(let x=0;x<5;x++){ setTimeout(function(){console.log(x);},1000); } 

call, it will not work in IE 11 because IE 11 does not support ES2015. 调用,它将无法在IE 11中使用,因为IE 11不支持ES2015。 But I can write this code in Typescript, compile, and use Babel to output to supposedly IE11-compatible code. 但是我可以用Typescript编写此代码,进行编译,然后使用Babel输出到据说与IE11兼容的代码。 Will Babel successfully recognize and fix this? Babel能否成功识别并解决此问题?

I realize this is relatively simple to test myself once I get an environment all set up but I kinda feel like this might just be the tip of the iceberg for what Babel can and cannot do. 我意识到,一旦设置了所有环境,就可以相对轻松地进行自我测试,但是我有点觉得这可能只是Babel可以做和不能做的冰山一角。 Just wondering if someone knows off the top of their head if or how this works and if there are some related closure-related things that Babel just cannot transform. 只是想知道是否有人知道它是否起作用或如何起作用,以及是否存在Babel无法改变的一些与闭合相关的事情。 Thanks in advance. 提前致谢。 ---Edit--- Passing the variable 'x' into the function actually yields an undefined error so I guess I was breaking some other rule there. ---编辑---将变量'x'传递到函数中实际上会产生未定义的错误,所以我想我在这里违反了其他一些规则。 But, after removing the anonymous function parameter to create a proper closure on 'x', I find that the Babel conversion does NOT properly log the value 0,1,2,3... At this point I guess I would disagree with those who are saying that it does properly convert this scenario. 但是,在删除匿名函数参数以在'x'上创建适当的闭包后,我发现Babel转换未正确记录值0、1、2、3 ...此时,我想我会不同意这些谁在说它确实可以转换这种情况。 Or I am totally missing something. 或我完全错过了一些东西。 (Thanks to the first person who replied with the link to babeljs.io/repl ! Very cool.) (感谢第一个回复了babeljs.io/repl链接的人!非常棒。)

Make sure you are using env aka es2015 preset. 确保您使用的是env aka es2015预设。

in babelrc : babelrc

{
   "presets": ["env"]
}

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

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