[Previous] [Next] [First] [Last]


Object Model

The framework for the JavaScript object model is built around

5.1 Functions

A function is a set of statements that performs a specific task. A function is defined by a function definition that specifies the function's name and the statements that it contains. A function is executed by a function call.

5.1.1 Definition

A function definition provides:

The syntax for defining a function is described in 6.4.10 Function Definition Statement. A function must be declared before it is invoked.

5.1.2 Call

A function call executes the statements in the function and optionally returns a value. It consists of a defined function's name, followed by a parameter list in parentheses.

The number of arguments in an call does not have to match the number of arguments in the function definition. Each argument in the call will be matched from left to right with the arguments in the definition. Any argument in the definition for which there is no argument in the call will be undefined. If there are more arguments in the call than in the definition, then the extra arguments are accessible within the function using the arguments array; see 5.1.4 The arguments array.

The value of a function call expression is the value of the expression following the return statement that returned control to the call, or undefined if there was no return statement or a return statement without an expression.

Note that JavaScript does not require function arguments to be of any particular type.

5.1.3 The caller property

Every function has a caller property that is a reference to the function that called it, if any. The caller proper non-null only within a function. The syntax is:

functionName.caller

If the function was not called from another function then caller is null. The function referred to by the caller property has a null caller property, even if the caller was itself called by another function.

5.1.4 The arguments array

The arguments of a function are maintained in an array. Within a function, you can address the parameters passed to it as follows:

functionName.arguments[i]

where functionName is the name of the function and i is the ordinal number of the argument, starting at zero. So, the first argument passed to a function named myfunc would be myfunc.arguments[0]. As for other arrays in JavaScript, the arguments array has a length property that indicates the number of elements in the array. Thus, within the body of the function, the actual number of arguments passed to a function is accessible as arguments.length .

Since a a function can be invoked with more arguments than the number of parameters in its definition, the arguments array provides a way to access the excess arguments in the call.

5.2 This

The special keyword this is used to refer to the current object. The current object is defined as follows:

5.3 Constructor Functions

A constructor is a function used to define new objects. In addition to the constructors for built-in objects, described in Chapter 7, "Built-in Functions and Objects", user-defined constructor functions create objects of the user's own definition.

A constructor function sets properties of this to create the new object's properties.

5.3.1 Object prototypes

Every object constructor (including built-in object constructors) has a prototype property. Setting a property of a constructor's prototype property creates a property shared by all objects created by the constructor.

prototypeProperty
          constructor.prototype.propertyName

Set the value of constructor.prototype.propertyName to define a property that is shared by all objects of the specified type, where constructor is the name of the constructor function and propertyName is the name of the property.

Examples

function str_rep(n) {
          var s = "", t = this.toString()
          while (--n >= 0) s += t
          return s
}

function new_rep(n) { 
          return "repeat " + this + " " + n + " times." 
}

String.prototype.rep = str_rep //add a rep() method to String
s1 = new String("a")
s2 = new String("b")
s2.rep = new_rep
println(s1.rep(3))
println(s2.rep(3))

The output of this example is:

aaa
repeat b 3 times.

5.3.2 Defining methods

A method is a function associated with an object. A function can be so associated in two ways:

You can define methods for an object type by including a method definition in the object constructor function.

The following syntax associates a function with an existing object:

object.methodName = functionName

where object is an existing object, methodname is the name being assigned to the method, and function_name is the name of the function.

The method can then be called in the context of the object as follows:

object.methodName(ArgumentList)

Example

To format and display the properties of the previously-defined Car objects, the user would define a method called displayCar based on a function called display by modifying the constructor function as follows:

function car(make, model, year, owner) {
          this.make = make
          this.model = model
          this.year = year
          this.owner = owner
          this.display = displayCar
}

For example, this function might look like this:

function displayCar() {
          println("A Beautiful " + this.year + " " + this.make 
          + " " + this.model)
}

Then the displayCar method can be called for each of the objects as follows:

car1.displayCar()
car2.displayCar()

5.4 Object Creation

A new object instance is created by using the new operator with a constructor, either one of the built-in object constructors described in Chapter 7, "Built-in Functions and Objects", or a user-defined constructor function, described in 5.3 Constructor Functions.

constructor:
          DateConstructor
          ArrayConstructor
          StringConstructor
          BooleanConstructor
          NumberConstructor
          UserDefinedConstructor

Example

The following user-defined constructor functions create a Person object with member properties name, age, and sex and a Car object with member properties make, model, year, and owner. Then, two Person objects are created with the new operator, and two Car objects are created. Notice the use of the objects john and fred as arguments to the Car constructor functions. This is an example of members of object type.

function Person(name, age, sex) {
          this.name = name
          this.age = age
          this.sex = sex
}

function Car(make, model, year, owner) {
          this.make = make
          this.model = model
          this.year = year
          this.owner = owner
}

john = new Person("John", 33, "M")
fred = new Person("Fred", 39, "M")

car1 = new Car("Eagle", "Talon TSi", 1993, john)
car2 = new Car("Nissan", "300ZX", 1992, fred)


Footnotes


[Previous] [Next] [First] [Last]