0

I am trying to understand someone else's codes. He has

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']}]
}

I am not sure if 'header' and 'input' are the object properties? What are the attributes under 'header' and 'input'

What do these do:

{'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'}],

I thought to declare object properties, we do

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

and I am not sure why he has so many atttriubes.

Thanks for the help guys!

2
  • Objects (and arrays) can be nested with any number of levels. A JSON prettifier might help to understand the structure. Commented Dec 19, 2012 at 21:05
  • header, input and image are arrays of objects (notice the square brackets: [ and ]). Commented Dec 19, 2012 at 21:05

2 Answers 2

3

header and input are indeed the object properties of taskAttributes, along with image as well.

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

Each of those is itself an array [] of objects {} with properties like name, display, type. This addresses the "what do these do" part of your question.

// 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': {...}}

So to access the first name beneath header for example, you would access its array key [0] as in:

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

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

There is one further level of nesting on the first array element of both headers and input which looks like:

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

That is yet another object referenced as options for each of those.

taskAttribtues.header[0].options.title1
// 'manager'
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, header, input and image are properties of the task.prototype.taskAttributes object, each of them containing an array of objects. Piping your code through http://jsbeautifier.org/ could help by accentuating the literal structure with indentation:

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']
    }]
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.