Classes and objects
To create a class, add a template from the Templates>Language>class menu. In the edit field, a function template with the name Class will be inserted. The class operator is not supported, therefore it is necessary to use such a construction.
function Class (name) {
this.name = name; // the class field
this.echo = function(text){ // the class method
return text;
};
}
The template contains the class with one field and one echo-function. You can create an object using the following code
var obj = new CLass();
A more illustrative example using classes and objects. Let's create a myNumber object that can store the number and increment it.
function MyNumber(n) {
this.n = n; // the class field
this.inc = function(){ // the method "increment the value"
n++;
};
this.get = function(){ // the method "get the value"
return n;
};
}
var myNumber = new MyNumber(0); // the object creating
myNumber.inc(); // the calling the inc() method three times
myNumber.inc();
myNumber.inc();
system.println('My number = '+myNumber.get()); // the writing to an output