1

I just started learning JS and programming one small game for training. But I get a weird error while executing this object:

mainState = {
    name : "GAME_STATE",

    Setup: function( context, settings ) {
    }


    HandleKeyDown: function( event ) {
    }

    Update: function( settings ){

    }

    Draw: function( context, settings ){

    }
} 

And all that FireBug says is:

SyntaxError: missing } after property list
HandleKeyDown: function( event ) {

Thank you for your help!

1
  • 1
    you must adding comma after every element in object mainState, not only name, and after function Setup, HandleKeyDown...Good luck Commented Sep 6, 2016 at 8:08

3 Answers 3

1

You need to put comma between properties (key and its value), otherwise it is not a valid JavaScript Object and hence this compilation error thrown by Firebug

SyntaxError: missing } after property list

Here is the correct way to do it

mainState = {
  name: "GAME_STATE",
  Setup: function(context, settings) {},
  HandleKeyDown: function(event) {},
  Update: function(settings) {},
  Draw: function(context, settings) {} // here no comma
}
Sign up to request clarification or add additional context in comments.

1 Comment

It is a javascript object, not a JSON.
1

In javascript, you can easily create javascript objects, but first you must know the notation:

var jsObj1 = {}; //A very basic initiation.
var jsObj2 = {
    a: "a",
    b: "b",
    c: "c"
};

Here, as you can see, there are commas between every field of a JS object. For your situation here, you have created functions for each of your JS object's fields' values but forgot to put commas:

var mainState = {

    name : "GAME_STATE",

    Setup: function( context, settings ) {

    },

    HandleKeyDown: function( event ) {

    },

    Update: function( settings ) {

    },

    Draw: function( context, settings ) {

    }

};

So this will work without any error.

1 Comment

Fairly identical to @gurvinder372's answer, no?
0

You need add commas:

mainState = {
    name : "GAME_STATE",

    Setup: function( context, settings ) {
    },


    HandleKeyDown: function( event ) {
    },

    Update: function( settings ){

    },

    Draw: function( context, settings ){

    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.