ES6 Classes: Unexpected token in script?
In ES2015, when using the class
syntax, you need to define instance variables either in the constructor or one of the methods (there is a proposal for the next iteration, ES2016, to allow for your syntax: ES Class Fields & Static Properties)
class Counter { constructor() { this.count = 0; setInterval(function() { this.tick(); }.bind(this), 1000); } tick() { this.count++; console.log(this.count); }}var c = new Counter();
Check out the fiddle:
3 years late so you probably figured it out, but I placed the count variable in the constructor
class Counter { constructor(count = 0) { this.count = count setInterval(function() { this.tick(); }.bind(this), 1000); } tick() { this.count ++; console.log(this.count); }}let counter = new Counter;counter.tick()
gave it more control by calling the tick function
class Counter { constructor(count = 0) { this.count = count; } tick() { let count = this.count; setInterval(function() { console.log(count++); }, 1000); }}let counter = new Counter;// counter.tick()
Perhaps it's the compiler issue. Check what version of Babel you're using.
In my case I missed the babel-preset-stage-0
dependency.