2012-12-19 92 views
1

我想了解別人的代碼。他JavaScript對象問題

task.prototype.taskAttributes = { 
    'header' : [ 
    {'name' : 'display_type', 'display' : 'Display Type', 'type' : 'select', 'options' : { 
     'default' : 'Default', 
     name1 : 'Peter', 
     name2 : 'Ted', 
    } 
    }, 
    {'name' : 'background', 'display' : 'Background', 'type' : 'image'}, 
    {'name' : 'background_position', 'display' : 'Background Position', 'type' : 'text'}, 
    {'name' : 'credit', 'display' : 'Background Credit', 'type' : 'text'}], 

    'input' : [ 
    {'name' : 'display_type', 'display' : 'Display Type', 'type' : 'select', 'options' : { 
     'default' : 'Default', 
     title1 : 'manager', 
     title2 : 'employee'} 
    }, 
    {'name' : 'background', 'display' : 'Background', 'type' : 'image'}, 
    {'name' : 'background_position', 'display' : 'Background Position', 'type' : 'text'}, 

    'image' : [{'name' : 'column', 'type' : 'select', 'options' : ['', 'left', 'right']}] 
} 

我不知道如果「header」和「input」爲對象的屬性? 什麼條件下「header」屬性和「input

做這些事:

{'name' : 'display_type', 'display' : 'Display Type', 'type' : 'select', 'options' : { 
    'default' : 'Default', 
    name1 : 'Peter', 
    name2 : 'Ted', 
} 
}, 
{'name' : 'background', 'display' : 'Background', 'type' : 'image'}, 
{'name' : 'background_position', 'display' : 'Background Position', 'type' : 'text'}, 
{'name' : 'credit', 'display' : 'Background Credit', 'type' : 'text'}], 

我想申報對象的屬性,我們做

attribute={header:'header', input:'input'} 

,我不知道爲什麼他有這麼多的屁股。

感謝您的幫助!

+0

對象(和數組)可以用任何數量的級別嵌套。 JSON美化器可能有助於理解結構。 – Bergi

+0

'header','input'和'image'是對象數組(注意方括號:''''和']')。 – lbstr

回答

4

headerinput和是確實taskAttributes對象屬性,連同image爲好。

taskAttributes = { 
    // Three object properties, each is an array 
    header: [], 
    input: [], 
    image: [] 
} 

每個那些本身與像namedisplaytype屬性的對象{}的陣列[]。這解決了「這些做什麼」你的問題的一部分。

// Example object element of the parent attribute arrays: 
// There are multiples of these objects for each property header, input, image 
{'name' : 'background', 'display' : 'Background', 'type' : 'image', 'options': {...}} 

所以訪問第一nameheader例如下面您就可以訪問它的數組鍵[0]爲:

taskAttributes.header[0].name 
// 'displayType' 

// And the third array element [2] 
taskAttributes.header[2].name 
// 'credit' 

有嵌套既headers的第一個數組元素的一個進一步水平和input看起來像:

// Property named options is an object... 
'options' : { 
    'default' : 'Default', 
    title1 : 'manager', 
    title2 : 'employee' 
} 

這是另一個對象引用爲每個人都有options

taskAttribtues.header[0].options.title1 
// 'manager' 
1

是,headerinputimagetask.prototype.taskAttributes對象的屬性,它們中的每含有對象的數組。通過http://jsbeautifier.org/管道代碼可以幫助用縮進加劇字面結構:

task.prototype.taskAttributes = { 
    'header': [{ 
     'name': 'display_type', 
     'display': 'Display Type', 
     'type': 'select', 
     'options': { 
      'default': 'Default', 
      'name1': 'Peter', 
      'name2': 'Ted', 
     } 
    }, { 
     'name': 'background', 
     'display': 'Background', 
     'type': 'image' 
    }, { 
     'name': 'background_position', 
     'display': 'Background Position', 
     'type': 'text' 
    }, { 
     'name': 'credit', 
     'display': 'Background Credit', 
     'type': 'text' 
    }], 
    'input': [{ 
     'name': 'display_type', 
     'display': 'Display Type', 
     'type': 'select', 
     'options': { 
      'default': 'Default', 
      'title1': 'manager', 
      'title2': 'employee' 
     } 
    }, { 
     'name': 'background', 
     'display': 'Background', 
     'type': 'image' 
    }, { 
     'name': 'background_position', 
     'display': 'Background Position', 
     'type': 'text' 
    }, 
    'image': [{ 
     'name': 'column', 
     'type': 'select', 
     'options': ['', 'left', 'right'] 
    }] 
}; 
+0

感謝您的幫助! +1 – FlyingCat