Javascript error message: ECMA-262 object initializers

I have simple Javascript and an error message. What should I actually change?
Scroll = {
init: function() {
jQuery.scrollUp(
{
scrollName: ‘scroll’,
topDistance: ‘20’,
topSpeed: 20,
animation: ‘fade’,
animationInSpeed: 100,
animationOutSpeed: 100,
scrollText: ‘To the top’,
activeOverlay: true,
}
);
}
}

Error message:
warning: trailing comma is not legal in ECMA-262 object initializers

What happens if you remove the last comma after activeOverlay:true?

I have read articles and I understand commas are an issue but how to know which comma is an issue?

I have removed comma and it is validated now.

Example another issue:
jQuery(“#myform”).validate(
{
rules: {
search1: “required defaultInvalid”,
},
messages: {
string1: “Error message.”
}
}

See this search1: “required defaultInvalid”,

If I remove , it will be correct without an error but , will post an error.

Well you do a comma to signify that something else is coming after it. You did a comma after the last item in that object, and then immediately ended it with }. Can you see why, logically, that’s invalid? A comma and then immediately ending it? If the object is done, don’t put another comma there and make the browser think that another key=>value is going to be inserted there! :slight_smile: .

So, as you posted I just remove also comma also in another example as object is ended. Right?

jQuery("#myform").validate(
   {
   rules: {
    search1: "required defaultInvalid",
    },
   messages: {
    string1: "Error message."
    }
   }


    search1: "required defaultInvalid",

Correct; right after the comma (,) you are closing the object with the bracket. If there is no more key=>value, then do not do a comma after the last one.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.