【发布时间】:2021-07-01 20:18:57
【问题描述】:
我正在尝试使用构造函数创建一个 GameState 类来分配我的变量。我不断收到错误:Uncaught SyntaxError: Unexpected token 'class' - 但我能找到的所有文档都说要这样做。我不确定出了什么问题。 MDN参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
我还在堆栈溢出上找到了这个解决方案,它看起来也像我所拥有的,但我仍然遇到错误。 ES6 Classes: Unexpected token in script?
我尝试注释掉我的所有代码并从 MDN 粘贴一个示例,它抛出了相同的错误 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
有人可以帮我吗?我对编程很陌生,但是所有文档看起来都正确,我仍然迷路了。下面的代码供参考。
class GameState {
constructor() {
this.board = [null, null, null, null, null, null, null, null, null];
this.whoseTurn = 1;
this.winner = null;
}
}
let gs = new GameState();
【问题讨论】:
-
class GameState {前面的代码是什么? -
你用的是什么浏览器?
-
如果您使用的是 Internet Explorer,难怪会出现错误……
标签: javascript