Object Literals

Shane Brinkman-Davis Delamore edited this page Mar 29, 2018 · 11 revisions

Object literals can be implicit, explicit-to-end-of-line(EOL), explicit-blocks, or traditional JavaScript-style:

# implicit comma after simple literals
a = foo: 1  bar: 2

# basic implicit form
a = foo: 1, bar: 2

# basic implicit block form
a =
  foo: 1
  bar: 2

# basic explicit block form
a = {}
  foo: 1
  bar: 2

# basic explicit to-eol form
a = {} foo: 1, bar: 2

# JavaScript form
a = {foo: 1, bar: 2}

Arrays of Objects

Sometimes you want an array of objects, not just one object.

a =
  {} foo: 1
  {} bar: 2
  {} baz: 3

a = [{foo: 1}, {bar: 2}, {baz: 3}]

Implicit object-literals with two or more object-literal-properties on one line are 'one-liners':

# implicit comma after simple literal
a =
  foo: 1  fod: 4
  bar: 2  bad: 4
  baz: 3  bud: 4

a =
  foo: 1, fod: 4
  bar: 2, bad: 4
  baz: 3, bud: 4

a = []
  foo: 1, fod: 4
  bar: 2, bad: 4
  baz: 3, bud: 4

a = []
  {} foo: 1, fod: 4
  {} bar: 2, bad: 4
  {} baz: 3, bud: 4

a = [
  {foo: 1, fod: 4}
  {bar: 2, bad: 4}
  {baz: 3, bud: 4}
]

Duplicate property names in object-literals start a new object in the list: (COMING SOON)

a =
  foo: 1
  bar: 100
  foo: 2
  foo: 3

a =
  {} foo: 1, bar: 100
  {} foo: 2
  {} foo: 3
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.