最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascriptjquery error "invalid object initializer" - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Jul 18, 2011 at 12:54 user429620user429620 1
  • 1 What do you think JSON is exactly? There is no JSON in your code snippet. – Álvaro González Commented Jul 18, 2011 at 12:57
Add a ment  | 

4 Answers 4

Reset to default 6

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.

JavaScript objects are key/value pairs:

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

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

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);

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

发布评论

评论列表(0)

  1. 暂无评论