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. (This is actually the script file for the Visitors example's Main component. You can look at the entire Visitors example in <DocRoot>/WebObjects/Examples/WebScript/Visitors.woa.)

	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. 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 "What Is a WebObjects Application?", WebObjects applications can contain three kinds of script files: a component script inside a .wo directory, an application script, and a session script. These three kinds of scripts create subclasses of the WebObjects classes WOComponent, WOApplication, and WOSession, respectively. As you'll learn later, you can also subclass other classes in WebScript, but doing so is rare.

Table of Contents Next Section