© 2005 Goetz Heller
Table of Contents Description Source Code Copyright Note backIn the first example we define a simple helloWorld class in the namespace universe and create an instance of it. All available methods of the class, the instance, and the namespace are called. In the second example, we demonstrate error handling when a class with an invalid classID is to be installed.
// ex 1: install a simple class
document.write('<'+'h4><'+'em>Install a Class<'+'/em><'+'/h4>');
// constructor
var fullClassName = 'universe.helloWorld';
var hello = function() {};
hello.prototype.print = function() { return 'Hello, World!';};
// provide for class information
hello.classID = new Function('return \''+fullClassName+'\';');
core.installClass(hello);
// test class
try {
var cls =eval(fullClassName);
document.write('Class \''+fullClassName+'\' successfully installed.\n');
document.write(cls.classID()+'.classID(): '+cls.classID()+'\n');
document.write(cls.classID()+'.toString(): '+cls.toString()+'\n');
document.write(cls.classID()+'.isClass(): '+cls.isClass()+'\n');
document.write(cls.classID()+'.isNameSpace(): '+cls.isNameSpace()+'\n');
document.write(cls.classID()+'.isInstance(): '+cls.isInstance()+'\n');
} catch(e) {
document.write('Failed to install class \''+fullClassName+'\'.\n');
}
try {
eval(fullClassName);
document.write('\n<'+'h5><'+'em>Instantiate Class and Call Methods<'+'/em><'+'/h5>');
var hw = core.createInstance(fullClassName);
document.write('hw.classID(): '+hw.classID()+'\n');
document.write('hw.toString(): '+hw.toString()+'\n');
document.write('hw.isClass(): '+hw.isClass()+'\n');
document.write('hw.isNameSpace(): '+hw.isNameSpace()+'\n');
document.write('hw.isInstance(): '+hw.isInstance()+'\n');
document.write('hw.getClass(): '+hw.getClass()+'\n');
document.write('hw.print(): '+hw.print()+'\n');
document.write('\n<'+'h5><'+'em>Looking at the Namespace<'+'/em><'+'/h5>');
var ns = (new core.classPath(fullClassName)).nameSpaceString();
document.write('core._isNameSpace(\''+ns+'\'): '+core._isNameSpace(ns)+'\n');
ns = eval(ns);
document.write(ns+'.toString(): '+ns.toString()+'\n');
document.write(ns+'.isClass(): '+ns.isClass()+'\n');
document.write(ns+'.isNameSpace(): '+ns.isNameSpace()+'\n');
document.write(ns+'.isInstance(): '+ns.isInstance()+'\n');
} catch(e) {}
// ex.2: try to install class with invalid classID
document.write('\n<'+'h4><'+'em>Install a Class With Invalid classID<'+'/em><'+'/h4>');
// constructor
fullClassName = 'my InvalidClass';
var mic = function() {};
mic.prototype.print = function() { return 'Hello, World!';};
// provide for class information
mic.classID = new Function('return \''+fullClassName+'\';');
try {
core.installClass(mic);
// test class
eval(fullClassName);
document.write('Class \''+fullClassName+'\' successfully installed.\n');
} catch(e) {
document.write('Failed to install class \''+fullClassName+'\'.\n');
}
// ex.3: try to install another class with invalid classID
document.write('\n<'+'h4><'+'em>Install Another Class With Invalid classID<'+'/em><'+'/h4>');
// constructor
fullClassName = 'my..2ndInvalidClass';
var mic2 = function() {};
mic.prototype.print = function() { return 'Hello, World!';};
// provide for class information
mic2.classID = new Function('return \''+fullClassName+'\';');
try {
core.installClass(mic2);
// test class
eval(fullClassName);
document.write('Class \''+fullClassName+'\' successfully installed.\n');
} catch(e) {
document.write('Failed to install class \''+fullClassName+'\'.\n');
}