user429620
user429620

Reputation:

javascript/jquery error "invalid object initializer"

I'm calling a function like this:

myfunc($tab, {'top-left', 'bottom-left'}, defaults.tabRounded);

The function definition is:

function myfunc(obj, properties, value) {

Yet I get the error "Invalid object initializer". Is this because of the json argument? Or something else?

Upvotes: 5

Views: 20499

Answers (4)

yan.kun
yan.kun

Reputation: 6908

You need to name the properties like { x: 'foo', y: 'bar' }, as these are always key-value pairs.

Upvotes: 0

bjornd
bjornd

Reputation: 22933

Probably you want to pass array, not object to the function:

myfunc($tab, ['top-left', 'bottom-left'], defaults.tabRounded);

Otherwise if you want to pass object you need to specify values for the keys. Something like:

myfunc($tab, {'top-left': 100, 'bottom-left': 100}, defaults.tabRounded);

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146380

JavaScript objects are key/value pairs:

{
    'top-left': 333,
    'bottom-left': 444
}

https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Using_Object_Initializers

Upvotes: 2

Joe
Joe

Reputation: 82574

Replace

myfunc($tab, {'top-left', 'bottom-left'}, defaults.tabRounded);

With

myfunc($tab, ['top-left', 'bottom-left'], defaults.tabRounded);

{'top-left', 'bottom-left'} is not an object, but {'top-left': 0, 'bottom-left': 10} is an object. I assumed you might have wanted an array instead of an object.

Upvotes: 6

Related Questions