对象中的 Javascript 对象

Javascript object within object

本文关键字:对象 Javascript      更新时间:2023-09-26

我在对象中创建对象时遇到了一些问题,它与语法相关,但似乎不记得我如何实现这一点。

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

我收到未捕获语法错误的语法错误:意外令牌 =

使用 : 将"属性"与其各自的值分开:

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

一些阅读:

  • http://www.dyn-web.com/tutorials/obj_lit.php
您需要

一个:而不是=作为默认值。

var ajaxRequest = {
that: null,
request: null,  
multiRun: null,
multiRunTimer: null,
defaults: {
    ext: '',
    url: '',
    type: "POST",
    dataType: "json",
    payload: null,
    beforeSend: 'handleBefore',
    error: 'handleError',
    complete: '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',
    complete: 'handleCompletion',
    pass: false,
    debug: false,
    multiRunBlock: false                
}}

正如它所说,您对=有问题。用户=分配变量,但对象中的属性应使用 :(就像其他属性一样(