Using Object Oriented Programming in your Javascript allows your code to be easier to maintain and develop. The documentation on this page is used as a suggestion - it is not necessary to develop your Javascript code in this way.
For an introduction to Object Oriented Programming, please see
JavaScript Object-Oriented Programming Part 1 and
JavaScript Object-Oriented Programming Part 1. These tutorials offer a nice introduction to creating Javascript classes.
Heres an example of a simple Javascript Person class. Notice that the class is defined in the same way as a method. pfirstname and plastname are parameters for the built in Constructor.
//----------------
//Person Class
//----------------
function Person(pfirstname, plastname) {
this.firstName = "";
this.lastName = "";
this.DoSomething= function(todo) {
//do something...
alert(this.firstName+" "+this.lastName+" has decided to "+todo);
}
}
//-----------------
//Test Function
//-----------------
function TestFunction(){
var me = new Person("Zedd","Zorandor");
me.DoSomething("Dance!");
}
Sometimes it can be useful to create a Static Class. Static classes are a single instance of a class that can be accessed globally from within your code. There exists only a single instance of this class which can be accessed directly.
You'll see this approach used in the built in TextSide utility functions as well as in the built in Parts.
Here is an example Static Class:
//-----------------
//Static MathLib class
//-----------------
var MathLib = new (function() {
//sample static variable
this.staticVariable = 0;
//sample static method
this.Max = function(a, b) {
if ( a >= b )
return a;
return b;
}
})();
//-----------------
//Test Function
//-----------------
function TestFunction(){
//example use of the static class
var biggest = MathLib.Max(50,250);
}