Objects, Properties, and Methods

JavaScript is based on a simple object-oriented paradigm. An object is a construct with properties; properties can themselves be other objects. You can associate functions with objects; these are known as an object's methods.

Arrays and Object Properties

A JavaScript object has properties associated with it. You define a property by assigning it a value. For example, suppose there is an object named car. You can give it properties named make, model, and year as follows:

car.make = "Ford"
car.model = "Mustang"
car.year = 67;

An array is an ordered set of values associated with a single variable name. Properties and arrays in JavaScript are intimately related; in fact, they are different interfaces to the same data structure. So, for example, the properties of the car object described above could also be accessed as follows:

car["make"] = "Ford"
car["model"] = "Mustang"
car["year"] = 67;

Equivalently, each of these elements can be accessed by its index, as follows:

car[0] = "Ford"
car[1] = "Mustang"
car[2] = 67;

This type of an array is known as an associative array, because each index element is also associated with a string value. To illustrate how this works, the following function displays the properties of the object, when you pass the object and the object's name as arguments to the function:

function dump_props(obj, obj_name) { 
    result = "" 
    for (i in obj) 
        result += obj_name + "." + i + " = " + obj[i] + "\n" 
    return result; 
} 

So, the function call dump_props(car, "car") would return the following:

car.make = Ford
car.model = Mustang
car.year = 67

Functions and Methods

A method is a function associated with an object. You define a method in the same way as you define a standard function. Then, use the following syntax to associate the function with an existing object:

object.methodname = function_name
where object is an existing object, methodname is the name you are assigning to the method, and function_name is the name of the function.

You can then call the method in the context of the object as follows:

object.methodname(params);
See also: function keyword, this keyword.

String Objects

A string object consists of a series of characters. It has one property, length, and three methods, substring, toUpperCase, and toLowerCase.

Length

The built-in length property returns the integer length of a string object.

Syntax

string.length

Example

If mystring is "netscape", then mystring.length returns the integer 8.

Substring

The built-in substring function returns a subset of a string object. If a < b, then it returns the subset starting at character a and ending at character b, considering the first character of the string to be character zero (0). If a > b, then it returns the subset starting at character b and ending at character a. If a = b, then it returns the empty string.

Syntax

string.substring(a, b);

Example

If string x is "netscape" then x.substring(0,3) or x.substring(3,0) returns "net", and x.substring(4,7) or x.substring(7,4) returns "cap".

toUpperCase

The ToUpperCase method of string objects converts the string to all upper case. For example, "alphabet".ToUpperCase yields "ALPHABET".

toLowerCase

The ToLowerCase method of string objects converts the string to all upper case. For example, "ALPHABET".ToLowerCase yields "alphabet".

Built-in Functions

Math Functions

TBD

Date and Time Functions

TBD

Other Functions

TBD