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 C 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 are 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 the 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:

 

   SE_CALLBACK( void )

circle(secontext se,sememcount argc)

{

   assert( argc==1 );

   seAssign(se, SE_THIS,SE_MEM("radius"), SE_ARGS,SE_NUM(0));

}

   SE_CALLBACK( void )

circleToString(secontext se,sememcount argc)

{

   sechar buffer[128];

 

   sprintf_sechar(buffer,UNISTR("circle of radius %d"),

                   (int)seGetNumber(se,SE_THIS,SE_MEM("radius")));

   sePutString(se, SE_RETURN,SE_VALUE, buffer, SE_PS_STRLEN);

}

 

SE_BEGIN_LIB_TABLE( 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_END_TABLE

 

. . .

 

seAddLibTable(se,circleTable,NULL);

 

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