-1

I'm using Spider Monkey to make a simple console chess game. However I keep getting the error SyntaxError: missing : after property id: in my enum declaration.

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

Heres the full code

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;
    }
}

Edit: Updated enum syntax as var declaration.

0x0byte
  • 109
  • 3
  • 11
  • Not a spidermonkey problem, That's not valid in any javascript engine – Jaromanda X Mar 20 '16 at 01:29
  • @JaromandaX What about it isn't valid? could you clarify. – 0x0byte Mar 20 '16 at 01:31
  • the syntax - while enum is a reserved word, I can't find any documentation (MDN, ES2015) that shows how to use it ... correction [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Future_reserved_keywords) documents it as a *future reserved keyword* – Jaromanda X Mar 20 '16 at 01:32
  • @JaromandaX Ive revised my syntax to the old style. The error still persists. – 0x0byte Mar 20 '16 at 01:37
  • Just a guess, but do your keys in the object need to be in quotes? 'WHITE'. – mrdeleon Mar 20 '16 at 01:52
  • I think you're using "class" all wrong - the body of a class contains methods and constructors - I see no reference to "vars" in the class [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) – Jaromanda X Mar 20 '16 at 02:08
  • Thanks for the help, that was it. Enums can't be defined inside the class body. – 0x0byte Mar 20 '16 at 03:32

2 Answers2

0

I guess enums can't be defined inside the class body. I was able to add them after the class definition.

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'
};
0x0byte
  • 109
  • 3
  • 11
0

The correct answer is here:

js classes instance properties

Static class-side properties and prototype data properties must be defined outside of the ClassBody declaration

Instance vars must be declared in the constructor with "this".

Zoltan K.
  • 1,036
  • 9
  • 19