ShowTable of Contents
This is a short howto for people who know how to code object oriented in other languages.
Create a class
function myClass(param1, param2) {
var private_member1 = param1;
var private_member2 = param2;
this.method1 = function () {
// do something
}
this.method2 = function(param1, param2) {
// do something
// you can access method1 and private member variables directly
}
// here goes the constructor code
}
Use the class
var object1 = new myClass("one", "two");
object1.method1();
object1.method2("another param1", "another param2");
var object2 = new myClass("three", "four");
// and so on
Use a class without creating it
As a sort of a singleton you can create a class to use it at runtime without the need to instanciate it with the "new" operator.
var myClass2 = new function() {
this.method1 = function(param) {
return "this is an example";
}
// and so on, just like the example above
}
You can use that class simply as follows:
alert(myClass2.method1());
Inheritance
It seems there are mutiple ways to emulate class inheritance in JavaScript.
I'm fine with the following method:
function rootObject() {
this.log(msg) {
print(msg);
}
}
function subObject() {
this.inheritFrom = rootObject;
this.inheritFrom();
}
Now you can use the log() method from the subObject class:
var o = new subObject();
o.log("test");
Creating an object
If you don't need to create multiple objects of the same time (using a class), you can create a single object instantly:
myObject = {}
myObject.member = "some string"
myObject.doWork = function() {
// a method
}
or
var myObject = {
member:"some string",
doWork:function() { ... },
"member with spaces in it's name":"some other string"
}
Accessing object's members and methods:
alert(myObject.member);
myObject.doWork();
alert(myObject["member with spaces in it's name"]);
Organizing with libraries
When you put serveral classes in one library A, and other classes in another library B, it's important to know that you have to import library A into B when you want to use objects from library A:
import libraryA;
Note: mind the case!
This is neccessary wether you import library A and B into your XPages as ressource or not.
true, false and falsy
A variable is only false, if it really has the boolean value of false.
But it is falsy, if it's
- false
- null
- undefined
- ""
- 0
- NaN
And in any other case it's true.
Useful little helpers
Return default value if a variable is not set
return myVariable || "some default value";
(this returns myVariable if it is not falsy, otherwise "some default value" is returned.)
Check if a variable is declared
if (typeof myVariable == "undefined") {
// is executed if myVariable was not declared yet
}
On the other hand, this:
if (!myVariable) {
}
only checks if the variable falsy (that means, it could be null or an empty string or NaN or so).
Return an object member only if the object already exists
return obj && obj.member;
Check if an object is an instance of a specific class
myObject instanceof MyClass
Check what members and methods an object has
for (member in myObject) {
alert(member.toString());
}
Check if an object as a specific member or method
obj.hasOwnProperty("name of member or method")
Extend built in or your own objects
You can extend any object (including built in objects) at any time:
if (!String.prototype.trim) {
String.prototype.trim = function() {return this.replace(/^s+|s+$/g, "");}
}
Get all parameters of a function call
The built in array "arguments" always contains all parameters of a function call.
var f = function() {
alert("my first parameter is: "+argument[0]);
alert("my second parameter is: "+argument[1]);
}
f("one", "two");
It doesn't matter if you wrote a parameter list in the function declaration like:
var f = function(a, b) {...}
See also
JavaScript Blast from Thomas Bahn