contents   index   previous   next



OBJECT CLASSES

 

An object class starts with a constructor function. A constructor function’s job is to initialize a new object of the object class. It can be written in Java using wrapper functions or implemented in JavaScript. When the user wishes to create a new object of your class, he calls your constructor function using the new operator, such as:

 

var a = new MyClass();

 

In this case, the constructor function is MyClass. Your constructor function will have a new blank object of its class provided to it as its this variable. The function can then add members to the this variable as appropriate for the task your object class is designed to perform. Here is a simple circle class constructor written in JavaScript:

 

function circle(radius)

{

   this.radius = radius;

}

 

With this constructor in your script, you can create a new circle object in JavaScript such as new circle(10). Although this example has implemented the circle constructor in JavaScript, you could also implement the circle constructor using the ScriptEase API, as we will demonstrate later in this chapter.

 

The particular parameters you pass to your constructor as well as how you set up your new object is determined by the object’s intended use. The main point to remember is that the this variable passed to your constructor is already a blank object of the constructor’s object class. All objects of a single class share common members via their prototype. This sharing is set up for your premade this variable passed to your constructor.

 

You designate the methods to share by putting them in the prototype member of the constructor function. All object’s of the constructor’s class have access to those members. Here is a simple script that uses a slightly extended version of the circle constructor:

 

function circle(radius)

{

   this.radius = radius;

}

 

function circle.prototype.toString()

{

   return "circle of radius " + this.radius;

}

 

var a = new circle(5);

Clib.puts(a.toString());

 

 

This is a simple program that will print circle of radius 5. Although we've implemented this in script form, you can do the same using the ScriptEase API. Here is the version that does so:

 

public void circle(SEContext se,int argc)

{

   assert( argc==1 );

   se.seAssign(SE.THIS,SE.MEM("radius"), SE.ARGS,SE.NUM(0));

}

 

public void circleToString(SEContext se,int argc)

{

   String str = “circle of radius “ +   

                (int)se.seGetNumber(SE.THIS,SE.MEM(“radius”));

 

   se.sePutString(SE.RETURN,SE.VALUE, str);

}

 

SELibraryTable[] circleTable = 

{

   SE.CLASS( "circle", “circle”, 1, 1, SE.SECURE, SE.DEFAULT),

      SE.PROTO

       SE.METHOD( "toString", “circleToString”, 0, 0,       

                  SE_SECURE, SE.DEFAULT),

      SE.END_PROTO,

   SE.END_CLASS

}

 

se.seAddLibTable(circleTable,this);

 

Notice that the circle function in both the JavaScript and ScriptEase API versions returns no value. It instead initializes the provided this variable. Constructors can return a value which will override the default preconstructed this variable. However, doing so requires you to do all of the initialization for the object you intend to return yourself.

 

Please refer to a JavaScript language book for more information on objects and object classes.

 


DYNAMIC OBJECTS