Javascript 尽管启用了实验性 js,但 ECMA 6 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32809728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ECMA 6 Not working although experimental js is enabled
提问by Emilio Grisolía
I have the latest Chrome version (45 and also Chrome Canary which is in version 47), both with the Experimental Javascript flag enabled. I want to use ECMA6, but it doesn't work. I don't know why. Is there any trick or other flag that must be enabled too?
我有最新的 Chrome 版本(45 和版本 47 的 Chrome Canary),两者都启用了实验性 Javascript 标志。我想使用 ECMA6,但它不起作用。我不知道为什么。是否也必须启用任何技巧或其他标志?
Every reserved word of ECMA6 (like import, class, or whatever) throws an "Uncaught SyntaxError: Unexpected reserved word" error in Chrome 45 and an "Uncaught SyntaxError: Unexpected token import" error in Chrome Canary.
ECMA6 的每个保留字(如导入、类或其他)在 Chrome 45 中抛出“未捕获的语法错误:意外的保留字”错误,在 Chrome Canary 中抛出“未捕获的语法错误:意外的令牌导入”错误。
I will appreciate any help. And, because I asked this a few months ago without getting any answer but "possible duplicate" of this Using ECMAScript 6, it is not. It does not solve my problem.
我将不胜感激任何帮助。而且,因为我几个月前问过这个问题,但没有得到任何答案,但“可能重复”使用 ECMAScript 6,所以它不是。它不能解决我的问题。
Thanks.
谢谢。
--- EDIT --- I want to use modules, since I like more the ecma6 modules than using Require or Common. And I also like the sugar syntax of classes, code looks better :)
--- 编辑 --- 我想使用模块,因为与使用 Require 或 Common 相比,我更喜欢 ecma6 模块。而且我也喜欢类的语法糖,代码看起来更好:)
回答by Sergey K
Modules are not yet natively supported in any browser. You will need to use a transpiler such as Traceuror Babel. Take a look at one of the following to help you get started:
任何浏览器均不支持模块。您将需要使用诸如Traceur或Babel 之类的转译器。查看以下内容之一以帮助您入门:
- Choose ES6 modules Today!
- ES6 In Depth: Using ES6 today with Babel and Broccoli
- Writing client-side ES6 with webpack
As for classes, you may be able to use these natively without having to go through a transpiler. You can check the compatibility table here to see which browsers support classes natively today:
至于类,您可以在本机使用它们而无需通过转译器。您可以在此处查看兼容性表,了解当今哪些浏览器原生支持类:
https://kangax.github.io/compat-table/es6/
https://kangax.github.io/compat-table/es6/
As of right now, you can see that the majority of browsers do not yet support classes natively. However, if you are using Babel or Traceur, that shouldn't be a concern.
到目前为止,您可以看到大多数浏览器本身还不支持类。但是,如果您使用的是 Babel 或 Traceur,则不必担心。
回答by Barbu Barbu
Add type="module"
to the scripttag. That should solve your problem.
添加type="module"
到脚本标签。那应该可以解决您的问题。
回答by jfriend00
Here are a couple feature lists that tell you what features work in Chrome 45.
以下是一些功能列表,可以告诉您哪些功能在 Chrome 45 中有效。
Chrome Feature Status: https://www.chromestatus.com/features
Chrome 功能状态:https: //www.chromestatus.com/features
ES6 compatibility matrix: https://kangax.github.io/compat-table/es6/
ES6 兼容性矩阵:https://kangax.github.io/compat-table/es6/
What you will find is that many features such as the class
sugar syntax work fine in Chrome 45, but require strict mode in order to be enabled.
您会发现许多功能(例如class
糖语法)在 Chrome 45 中运行良好,但需要使用严格模式才能启用。
For example, if you run this jsFiddle that uses class
in Chrome 45 or greater, it will work: http://jsfiddle.net/jfriend00/xd56k8n3/. If you run it outside of strict mode, it reports Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
.
例如,如果您运行class
在 Chrome 45 或更高版本中使用的这个 jsFiddle,它将起作用:http: //jsfiddle.net/jfriend00/xd56k8n3/。如果在严格模式之外运行它,它会报告Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
.
Modules do not look like they yet have support in any browser.
模块看起来还没有在任何浏览器中得到支持。
FYI, one common way to write code now in ES6 is to use a transpiler like Babelor Traceurthat you feed ES6 code into and it converts it to ES5-compatible code that runs in current browsers. You get to write in ES6, but have compatibility with current browsers.
仅供参考,现在在 ES6 中编写代码的一种常见方法是使用诸如Babel或Traceur 之类的转译器,将 ES6 代码输入其中,并将其转换为在当前浏览器中运行的 ES5 兼容代码。您可以使用 ES6 编写,但与当前浏览器兼容。