【问题标题】:Javascript SpiderMonkey SyntaxError: missing : after property id:Javascript SpiderMonkey SyntaxError: missing : after property id:
【发布时间】:2016-03-20 01:23:39
【问题描述】:

我正在使用 Spider Monkey 制作一个简单的控制台国际象棋游戏。 但是,我在枚举声明中不断收到错误 SyntaxError: missing : after property id:

SyntaxError: missing : after property id:
ChessPiece.js:4:5      var Color = {
ChessPiece.js:4:5 ............^

完整代码

class ChessPiece
{
    var Color = {
        WHITE : 'W',
        BLACK : 'B'
    };

    var Piece = {
        PAWN : 'P',
        ROOK : 'R',
        KNIGHT : 'N',
        BISHOP : 'B',
        QUEEN : 'Q',
        KING : 'K'
    };

    constructor(color, piece)
    {
        this.color = color;
        this.piece = piece;
    }

    toString()
    {
        return this.color + this.piece;
    }
}

编辑:将枚举语法更新为 var 声明。

【问题讨论】:

  • 不是蜘蛛猴问题,这在任何 javascript 引擎中都无效
  • @JaromandaX 无效怎么办?你能澄清一下吗?
  • 语法 - 虽然 enum 是保留字,但我找不到任何说明如何使用它的文档(MDN,ES2015)...更正MDN 将其记录为 未来保留关键字
  • @JaromandaX 我已经将我的语法修改为旧样式。错误仍然存​​在。
  • 只是一个猜测,但是您在对象中的键是否需要用引号引起来? '白色'。

标签: javascript spidermonkey


【解决方案1】:

我猜枚举不能在类体内定义。我能够在类定义之后添加它们。

class ChessPiece
{
    constructor(color, piece)
    {
        this.color = color;
        this.piece = piece;
    }

    toString()
    {
        return this.color + this.piece;
    }
}

ChessPiece.Color = {
    WHITE : 'W',
    BLACK : 'B'
};

ChessPiece.Piece = {
    PAWN : 'P',
    ROOK : 'R',
    KNIGHT : 'N',
    BISHOP : 'B',
    QUEEN : 'Q',
    KING : 'K'
};

【讨论】:

  • 问题不在于枚举。看我的回答。
【解决方案2】:

正确答案在这里:

js classes instance properties

静态类端属性和原型数据属性必须是 在 ClassBody 声明之外定义

实例变量必须在构造函数中用“this”声明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-08-24
    • 2016-12-20
    相关资源
    最近更新 更多