October 7, 1999 - Object Types | WebReference

October 7, 1999 - Object Types

Yehuda Shiran October 7, 1999
Object Types
Tips: October 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

JavaScript supports two different types of objects. Built-in objects, such as Math, that you cannot modify. PI, for example, is a property of the Math object and obviously cannot be modified. User-defined objects are created by the author. The course object discussed on October 6, 1999, is a user-defined object.

An object can be a property of another object, thus creating an object hierarchy. Suppose an object a has two object properties, b and c. The object b has two properties, d and e, while the object c has four properties, f, g, h, and i. Let's assume that d is 16, e is 42, f is true, g is "king", h is 13, and i is 10. The following expressions are references to various properies in this object hierarchy:

alert(a.b.d) // prints 16
alert(a.b.e) // prints 42
alert(a.c.f) // prints true
alert(a.c.g) // prints king
alert(a.c.h) // prints 13
alert(a.c.i) // prints 10

A property belongs to an object, and only to one object. A variable may be named exactly like a property of an object. The following statement is valid:

d = a.b.d;

However, a variable cannot have the same name as an object at the same level (scope). The following statement is not valid:

a = a.b.d;

It's good practice to avoid repeating names.