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!

Bahadir Tasdemir
10.9k4 gold badges54 silver badges63 bronze badges
asked Sep 6, 2016 at 7:58
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
}
mplungjan
180k29 gold badges183 silver badges246 bronze badges
answered Sep 6, 2016 at 8:00
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.

answered Sep 6, 2016 at 8:14

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 ){
 }
} 
answered Sep 6, 2016 at 7:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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