Objects

JavaScript has objects and is object-oriented, but in an unusual way (more on that later).

Simple objects

Simple objects are like dictionaries or hashes in other languages. They support key-value storage and access to attributes. Here’s how to create a simple object.

js> obj = {}
[object Object]

js> typeof obj
object

We can store attribute values in a simple object (or any object in the doc:object-tree).

js> obj.s = 'hi'
hi
js> obj.i = 10
10

We can get these values back again.

js> obj.s === 'hi'
true
js> obj.i == 10
true

Missing attributes

It’s not an error to ask for something that’s not there. We get undefined.

js> obj.dne === undefined
true

We still get undefined if we set the value to undefined.

js> obj.undef = undefined
undefined
js> obj.dne === undefined
true

You can use null to signal that there is a value, but that it is None.

js> obj.none = null
null
js> obj.none === null
true

hasOwnProperty

You can use hasOwnProperty to help figure out why an object has an undefined attribute. It’s also useful when inspecting the doc::object-tree. Often, however, it’s better to write your code so you don’t need to do this (for example by using null).

js> obj.hasOwnProperty('dne')
false
js> obj.hasOwnProperty('undef')
true
js> obj.hasOwnProperty('none')
true

Object literals

You can create a simple object by placing key-value pairs in the curly braces.

js> someone = {
  >     'name': 'Joe Doe',
  >     'age': 43
  > }
[object Object]

js> someone.age
43

Take care not to put a trailing semicolon in the object literal. It will work in Firefox but not in Internet Explorer.

Arrays

An array is a list of items. You can put anything is as a value for the list. Use square brackets to create an array.

js> array = []

Arrays expand to accomodate the data you store in them. You can even leave gaps.

js> array[0] = 'zero'
zero
js> array[3] = 'three'
three

An array turned into a string consists of the string on its entries joined by commas.

js> array
zero,,,three
js> array[1] === undefined
true

Array literals

As with simple objects, simply place the values between the square brackets, separated by commas.

js> seasons = ['spring', 'summer', 'autumn', 'winter']
spring,summer,autumn,winter

As with simple objects, beware of trailing commas and missing entries. This will work in some browsers and not others.

JSON

JSON,stands for JavaScript Simple Object Notation. It is very much one of the best parts of JavaScript. It is the fat-free alternative to XML, and is widely used in AJAX (instead of XML). JSON objects are object and array literals constructed using only simple objects, arrays, strings, true, false and null.

If your code accepts JSON objects then it will be a lot easier to use it with AJAX or otherwise integrate it with other systems.

Many programming languages have JSON libraries. You don’t have to use JavaScript to use JSON.

To provide standards, there are rules on how to write a JSON objects. Many applications and programming languages will generate valid JSON for you. I find YAML a convenient way of authoring JSON data.