admin管理员组

文章数量:1313001

I'm having a few issues with creating an object within an object, it's syntax related but can't seem to remember how I can achieve this.

ajaxRequest = {
that: null,
request: null,  
multiRun: null,
multiRunTimer: null,
defaults={
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    plete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
}}

I get a syntax error of Uncaught SyntaxError: Unexpected token =

I'm having a few issues with creating an object within an object, it's syntax related but can't seem to remember how I can achieve this.

ajaxRequest = {
that: null,
request: null,  
multiRun: null,
multiRunTimer: null,
defaults={
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    plete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
}}

I get a syntax error of Uncaught SyntaxError: Unexpected token =

Share Improve this question edited Dec 15, 2011 at 15:35 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 15, 2011 at 15:34 DavidDavid 36.5k14 gold badges51 silver badges80 bronze badges 4
  • 4 Have a look how you assign values to the other properties. – Felix Kling Commented Dec 15, 2011 at 15:36
  • use ":" instead of "=" in the code after "defaults" – Kashif Khan Commented Dec 15, 2011 at 15:36
  • When you're within an object, assign properties using a colon (:) instead of an =. – Cᴏʀʏ Commented Dec 15, 2011 at 15:36
  • Use jslint or something that uses it, like jsfiddle. – Bakudan Commented Dec 15, 2011 at 15:36
Add a ment  | 

3 Answers 3

Reset to default 8

Use : to separate 'properties' from their respective values:

defaults: {
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    plete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
}}

Some reading:

  • http://www.dyn-web./tutorials/obj_lit.php

You need a : instead of = for defaults.

var ajaxRequest = {
that: null,
request: null,  
multiRun: null,
multiRunTimer: null,
defaults: {
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    plete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
  }
};
ajaxRequest = {
that: null,
request: null,  
multiRun: null,
multiRunTimer: null,
defaults: {
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    plete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
}}

As it says, you have an issue with the =. User = to assign a variable, but properties within an object should use : (just like the rest of your properties)

本文标签: jqueryJavascript object within objectStack Overflow