MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Chapter 17. JavaScript 4: Objects and Arrays

Table of Contents

Introduction
Arrays
Introduction to arrays
Indexing of array elements
Creating arrays and assigning values to their elements
Creating and initialising arrays in a single statement
Displaying the contents of arrays
Array length
Types of values in array elements
Strings are NOT arrays
Variables — primitive types and reference types
Copying primitive type variables
Copying reference type variables
Array activities
Arrays concept map
The JavaScript object model
Introduction to the JavaScript object model
Objects in JavaScript
Naming conventions
Creating objects and variables referring to objects
Object properties and methods
Some important JavaScript objects
The 'Triangle' object
User defined objects
Implementing instance variables and methods
The JavaScript object search chain
Object activities
Arrays as objects
Array method: join
Array method: sort
Array method: reverse
Single and multi-dimensional arrays
Objects as associative arrays
Enumerating associative arrays with FOR/IN loop statements
Using FOR/IN for numerically index arrays
Activities and further work
Review Questions
Discussion Topics
Answers
Discussions of Exercise 1
Discussions of Exercise 2
Discussions of Activity 1
Discussions of Activity 2
Discussions of Exercise 3
Discussions of Activity 3
Discussions of Activity 4
Discussions of Activity 5
Discussions of Activity 6
Discussions of Activity 7
Discussions of Activity 8
Answers to Review Questions
Contribution to Discussion Topics

Introduction

Most high level computer programming languages provide ways for groups of related data to be collected together and referred to by a single name. JavaScript offers objects and arrays for doing so.

JavaScript arrays are rather different from arrays in many programming languages: all arrays are objects (as in many other languages), but they are also associative arrays. Also, all objects can also be used as if they, too, are arrays.

This unit is organised into four sections. It introduces arrays and objects separately, then considers arrays as objects, then finally considers objects as (associative) arrays. Many important concepts are covered in this unit, although much of the object technology concepts have been introduced in earlier units.

When working with variables, an important distinction has to be made: is the variable contain the value of a primitive type, or does it contain a reference to a (non-primitive) collection of data.

A thorough grounding in the concepts covered in this chapter is necessary to both be able to understand the sophisticated Javascipt scripts written to support complex websites, and to be able to begin developing JavaScript solutions yourself for real world problems. It is important to work through examples until you understand them; write your own programmes that use and test your learning. Programming is learnt through doing as much, or more so, than by reading.