Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Objects in WebScript

In WebScript, you work entirely with objects. An object is composed of data (called instance variables) and a set of actions that act upon that data (called methods). All variables that you declare are objects, and all values that a method returns are objects. There are no simple data types like int or char in C.

Each file that you write in WebScript defines an object. The definition of an object is called a class. A class specifies the instance variables that will be created for each object and the methods that the object will be able to perform. To create an object, you create an instance of a class (or instantiate a class).

For example, the following is a typical WebScript file.

id number, aName;

- awake {
if (!number) {
number = [[self application] visitorNum];
number++;
[[self application] setVisitorNum:number];
}
return self;
}

- recordMe {
if ([aName length]) {
[[self application] setLastVisitor:aName];
[self setAName:@""]; // clear the text field
}
}
Instance variables are declared at the top of the script file. In the example above, number and aName are instance variables of type id. An object's behavior is defined by its methods. awake and recordMe are examples of methods.

When you define a new class, you subclass an existing class. Subclassing gives you access not only to the variables and methods that you explicitly define but also to the variables and methods defined for the existing class (called the superclass). As you learned in the chapter "", WebObjects applications can contain four kinds of script files: a component script inside a .wo directory, an application script, a session script, and a direct action script. These four kinds of scripts create subclasses of the WebObjects classes WOComponent, WOApplication, WOSession, and WODirectAction, respectively. As you'll learn later, you can also subclass other classes in WebScript, but doing so is rare.

Table of Contents Next Section