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!
-
1you must adding comma after every element in object mainState, not only name, and after function Setup, HandleKeyDown...Good luckViktor Mezhenskyi– Viktor Mezhenskyi2016年09月06日 08:08:48 +00:00Commented Sep 6, 2016 at 8:08
3 Answers 3
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
}
1 Comment
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
You need add commas:
mainState = {
name : "GAME_STATE",
Setup: function( context, settings ) {
},
HandleKeyDown: function( event ) {
},
Update: function( settings ){
},
Draw: function( context, settings ){
}
}