Reputation: 5195
I found this code in a book:
class Animal(){
constructor(){
this.legs = 4;
this.eyes = 2;
this.say = "Huh?"
}
speak(){
console.log(this.say)
}
}
The above should be the right way to define a class but I can't use this in FF or chrome. I got SyntaxError: class is a reserved identifier
Also I was playing around with modules. I put
export let hello = "hello from the first module"
in a firstModule.js file
than I put
import {hello} from "firstModule"
console.log(hello)
in a main.js file
and i put <script type="text/javascript" src="main.js"></script>
in the index file and i got SyntaxError: modules are not implemented yet
I guess I'm having problems using es6 syntax that I thought I will be able to esspecially if use babeljs
I hope you could see in this babel "try it out" link that I attempted to use the class statement but i got an error Unexpected token (1:15)
.
How can use es6 stuff? I thought babel was supposed to translate es6 to es5. what am i doing wrong?
I also put <script type="application/javascript;version=1.7">
in the html file
Upvotes: 4
Views: 2031
Reputation: 23871
It should be class Animal
instead of class Animal()
.
To use ES6 code in your browser before it supports what you are using, you need babel to transpile the code to ES5. For example, in browser
<script type="text/babel" src="main.js"></script>
To play around with modules, you could try them with babel-node
, or could pack js files using webpack and load the result in browser.
Upvotes: 1