<script src="JSClassPath/JSClass.js"/> <script> //JSClass code </script>
The browsers/platforms support list is not maintained in this document.
Browser.name // browser name including relevant version Browser.version // detailed version info Browser.DOM // boolean, true if browser is DOM compatible Browser.platform // name of platform/OS Browser[name] // boolean , e.g. Browser.IE5, Browser.NS4 Browser[platform] // boolean , e.g. Browser.WIN , Browser.MAC
JSClass JSCDo not delete or override the JSClass object , as practically all functionality depends on it.
JSC.addInitEvent(functionReference1) JSC.addLoadEvent(function(){ //code to be executed }) JSC.addAfterLoadEvent(functionReference3) JSC.addOnunloadEvent(functionReference4)Do not override the JavaScript onload nor onunload event , as both are internally used by JSC.
function TheClass(){} // correct TheClass=function TheClass(){} // correct, and the safest TheClass=function(){} //incorrect!!! TheClass=function(){} TheClass.Name="TheClass" // correctNote that functions with the function name(){} notation are forward referenceable in JavaScript. So the syntax className=function className is the safest , as it eliminates the possibility of forward referencing the function. Beware of mismatching the names though.
TheClass=function TheClass(){ // constructor // initializing code } TheClass.Extends(JSC) JSC.addLoadEvent(function(){ var instance = new TheClass() // instantiating TheClass })Note that there is only one constructor per class; there is no way to overload constructors (see "Overloading").
var instance = new TheClass()Once an instance is created, there are alternative ways of accessing it (see "UID").
Class1.Extends(JSClass) // baseclass Class2.Extends(Class1RefOrString) // e.g. string would be "Class1" Class3.Extends(Class2RefOrString) // etc.It's only possible to pass a classreference as the argument if the class object already exists (e.g. JSClass). Otherwise you pass a string with the name of the class, and the class will be extended when available onload.
TheClass=function TheClass(x,y){ Super(x,y) //calls the parent class constructor } TheClass.Extends(TheSuperClass) // AND TheClass.Public.method=function(){ Super() // calls the Super method }If no Super call exists in a constructor, JSClass inserts an empty Super call as the first statement. Note that a Super call does not need to be the first statement in the constructor.
var obj = new package1.package2.TheClass() //e.g.
Import(pathString1,pathString2,...n) Import("folder/file.js")The class path/root folder, is by default the same as for the file including JSClass. You can explicitly set the class path with the setClassPath() method.
Import.setClassPath(classPathString) // e.g. js/classes/When importing classes, packages are used.
Import(thePackageString) Import("package1.package2.TheClass") //e.g.The above example would import 'theClassPath/package1/package2/TheClass.js'.
Import("thePackage.*")Since there is no direct way of importing all classes in the package, what actually happens in the above example is that the '_Package.js' file in the package folder is downloaded. In the package file you are free to import whatever classes are needed for the package.
<< _Package.js >> Import( "thePackage.Class1", "thePackage.Class2", "thePackage.Class3" )Due to security issues in JavaScript, there is no way to ensure that all of the classes imported reside in the package, nor is there an automatic way of keeping the '_Package.js' file up-to-date. That would have to be done manually or with a perl script, for instance.
Import("thePackage.OtherClass") TheClass=function TheClass(arg){ Super(arg) } InlineWindow.Extends("OtherClass") // is guaranteed by the importJSC keeps track of classes imported, preventing duplicate imports.
Private was supported in previous versions of JSClass, but is not anymore. The implementation was too complex and substantially degraded performance.
Note that the these accessors are mainly for encouraging proper OO programming. Since everything is public in JavaScript, protected only has semantical meaning . However, a JSC debugger/error module could be used to warn about improper use.TheClass.Public={ w:12 } TheClass.Protected={ x:34 }You access methods/properties either with the this object or directly (which you can't do in normal JavaScript).
TheClass=function TheClass(){ this.x=3 y=[] // same as this.y=[], y defined outside method() } TheClass.Protected={ y:null, method:function(){} }Note that arrays and object objects must be initialized/created in the constructor. If you were to create them in the accessor object, they would be shared for all instances.
TheClass.Static={ Protected:{ num:3, obj:{} }, Public:{ method:function(){} } }In static methods and the static constructor (see"The Static Constructor"), you can access static methods and properties directly, explicitly or via the Static keyword.
TheClass.Static.Public.method=function(){ num=4 TheClass.num=5 // explicit, works everywhere Static.num // works in static/instance methods/constructors }Static methods and properties are not inherited nor copied down the inheritance chain and only exist for the defining class. So for all intents and purposes you have to use the explicit notation in subclasses, inner classes and outside the class.
TheClass.method()
TheClass.Static=function(){ // should be anonymous function method() } TheClass.Static.Protected={ method:function(){...} }The Static constructor provides an easy way of doing class initialization. It can be viewed as a load event for the class itself, as you can instantiate classes in it etc.
OuterClass=function OuterClass(){ } OuterClass.Extends(JSC) OuterClass.InnerClass:function(){ ... } OuterClass.InnerClass.Extends(JSC)Note that inner classes in JSClass are static, and are referenced the same way as static methods are.
OuterClass=function OuterClass(){ this.innerInstance = new Static.InnerClass() //OR this.innerInstance = new OuterClass.InnerClass() }Inner classes should reside in the same classfile as the outer class.
TheClass.Applies(decoratorRefORString) // OR TheDecorator.Decorates(classRefORString)Decorators should be functions, and named the same way as classes.
TheDecorator=function TheDecorator(){} TheDecorator.Decorates("TheClass") TheDecorator.Protected={ property:null } TheDecorator.Public={ method:function(o){ ... } } TheClass.Applies(TheDecorator)Class decorators are applied before any classes are parsed by JSClass, so extending a decorated class gives the subclass all of the decorated methods/properties.
Right now decorating decorators is possible, but needs to happen in the correct order, i.e. it's not done asynchronously.
Instances can be decorated using applyDecorator or Decorates.theInstance.applyDecorator(TheDecorator) TheDecorator.Decorates(theInstance,anotherInstance,...,n)
window.foo=4 TheClass=function TheClass(foo){ // == "bar" foo // == 9 ... refers to [this.foo] .. NOT the foo argument this.foo // == 9 var foo = 0 // will refer to [this.foo] aswell foo // == 0 this.foo // == 0 } TheClass.Public={ foo:9 } new TheClass("bar")Higher precedence variables hide lower precedence variables. You can always access instance properties and methods using the this object, and global variables through the window object.
JSC.addNative("instanceOf")Two native methods - toString and valueOf , are added by default, as they are cross-browser.
JSC.addNative("instanceOf") TheClass=function TheClass(){ ... } TheClass.Public={ instanceOf:function(){ ... }, // will be parsed toString:function(){ ... } // will be parsed, default native }
TheClass=function TheClass(){ var className = Class.name var SuperName = Class.Super.name var scopeName = Class.Scope.name var baseName = Class.BaseClass.name }Super is a reference to the Super Class object.
instance.getClass() // returns the class reflection object
var UID=instance.getUID()The inherent UID property is accessible also as a copy in constructors, but only in constructors.
TheClass=function TheClass(){ var id=UID+Class.getName() }You can also get an instance reference from it's UID by using the getInstance method.
var instance = JSC.getInstance(number)You can get a string representation of the instance through the getGlobal method. This string can then be evaluated to a global reference of the instance. This is useful in cases where you can't pass the instance reference directly, such as in setInterval.
var globalInstanceRefString = instance.getGlobal()To get an array containing all the instances of a class, use the getInstances method.
var instances = instance.getInstances(deep) // if deep == true then all subclass instances are included
obj["is"+className] // true or undefined obj.isTheClass // e.g. if(obj&&obj.isTheClass) //example of fully valid checkAn alternative way is using the instanceOf method:
var bool=obj.instanceOf(TheClassRefORString)Only use instanceOf if you know you have a real class instance, as only those have the method.
instance.destroy()Destroy simply loops through all methods/properties of the instance, setting them to null . The instance then becomes an empty object, but references to it still remain.
TheClass.Static={ Protected:{ finalize:function(){ var instances=getInstances() for(var i=0;i< instances.length;i++){ instances[i].destroy() } } } }Marking data to be garbage collected is best done by setting it to null. For instances you can automatically do this by calling the destroy method (see "Destroy").