Skip to main content link. Accesskey S

XPages Wiki

Submit Search

YouAtNotes XPages Wiki


Home > Server JavaScript > How to use object oriented server side javascript

How to use object oriented server side javascript

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 
  }

}


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");


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.
See what's possible with Xpages.
Have a look at our ServiceCommunicator website
and the YouAtNotes Support.
Use  searchlotus.com  for news in the Web related to Lotus Notes and Domino,
and to search those sites.
Check  youatnotes.com  for great Lotus Notes, Domino and XPages software.
Did this wiki help you?
Did this saved you time? Express your gratitude by making a donation:
PayPal - The safer, easier way to pay online!