view core-javascript/objects.rst @ 150:5118d0e38ba4 core-javascript-latex

$ hg merge core-javascript # into core-javascript-latex
author jf3243
date Tue, 01 Nov 2011 08:58:38 +0000
parents cff48ce99b1f
children d4753a63211b
line wrap: on
line source

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.

.. code-block:: javascript

   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`).

.. code-block:: javascript

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

We can get these values back again.

.. code-block:: javascript

   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.

.. code-block:: javascript

   js> obj.dne === undefined
   true

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

.. code-block:: javascript

   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.

.. code-block:: javascript

   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).

.. code-block:: javascript

   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.

.. code-block:: javascript

   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.

.. code-block:: javascript

   js> array = []

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

.. code-block:: javascript

   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.

.. code-block:: javascript

   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.

.. code-block:: javascript

   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 <http://www.json.org/>`_,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 <http://www.json.org/xml.html>`_, 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.