尝试在 es6 中使用 Class 关键字和模块
trying to use the Class keyword and modules in es6
我在一本书中找到这段代码:
class Animal(){
constructor(){
this.legs = 4;
this.eyes = 2;
this.say = "Huh?"
}
speak(){
console.log(this.say)
}
}
上面应该是定义 class 的正确方法,但我不能在 FF 或 chrome 中使用它。我得到了 SyntaxError: class is a reserved identifier
我也在玩模块。我放
export let hello = "hello from the first module"
在 firstModule.js 文件中
比我说的
import {hello} from "firstModule"
console.log(hello)
在 main.js 文件中
然后我将 <script type="text/javascript" src="main.js"></script>
放入索引文件中,我得到了 SyntaxError: modules are not implemented yet
我想我在使用 es6 语法时遇到了问题,我认为如果使用 babeljs 就可以解决这个问题
我希望你能在这个 babel "try it out" link 中看到我试图使用 class 语句但是我得到了一个错误 Unexpected token (1:15)
.
如何使用es6的东西?我认为 babel 应该将 es6 翻译成 es5。我做错了什么?
我也把<script type="application/javascript;version=1.7">
放在html文件里
应该是class Animal
而不是class Animal()
。
要在浏览器支持您正在使用的内容之前在浏览器中使用 ES6 代码,您需要 babel 将代码转换为 ES5。例如,in browser
<script type="text/babel" src="main.js"></script>
要尝试使用模块,您可以尝试使用 babel-node
,或者可以使用 webpack 打包 js 文件并在浏览器中加载结果。
我在一本书中找到这段代码:
class Animal(){
constructor(){
this.legs = 4;
this.eyes = 2;
this.say = "Huh?"
}
speak(){
console.log(this.say)
}
}
上面应该是定义 class 的正确方法,但我不能在 FF 或 chrome 中使用它。我得到了 SyntaxError: class is a reserved identifier
我也在玩模块。我放
export let hello = "hello from the first module"
在 firstModule.js 文件中
比我说的
import {hello} from "firstModule"
console.log(hello)
在 main.js 文件中
然后我将 <script type="text/javascript" src="main.js"></script>
放入索引文件中,我得到了 SyntaxError: modules are not implemented yet
我想我在使用 es6 语法时遇到了问题,我认为如果使用 babeljs 就可以解决这个问题
我希望你能在这个 babel "try it out" link 中看到我试图使用 class 语句但是我得到了一个错误 Unexpected token (1:15)
.
如何使用es6的东西?我认为 babel 应该将 es6 翻译成 es5。我做错了什么?
我也把<script type="application/javascript;version=1.7">
放在html文件里
应该是class Animal
而不是class Animal()
。
要在浏览器支持您正在使用的内容之前在浏览器中使用 ES6 代码,您需要 babel 将代码转换为 ES5。例如,in browser
<script type="text/babel" src="main.js"></script>
要尝试使用模块,您可以尝试使用 babel-node
,或者可以使用 webpack 打包 js 文件并在浏览器中加载结果。