creating a jsolait module

Again, jsolait was created to simplify JavaScript development.

So, creating a module should be simple. A module should be placed into its own file. The filename should be the same as the module's name and postfixed with .js.
E.g. the code below would be placed into a file called tutorial.js:


Module("tutorial", "0.0.1", function(mod){
    var codecs = imprt("codecs");

    mod.testCodecs = function(data){
        alert(codecs.listEncoders());
        var s1 = "Hello jsolait";
        var s2 = s1.encode("base64");
        alert("'%s' encoded using base64 is '%s'".format(s1,s2));
    };
});

To create a module we will use jsolait's Module(name, version, scope) function. As you can see above it expects 3 parameters.

  • The first one is the name of the module.
  • The second one is the module's version.
  • The third one is a function which defines the module's scope. This function has a parameter called mod which represents the modules public interface.

At first this might look a bit awkward but using a function is the only way in JavaScript to create new scopes. This enables us to define objects visible only within that scope. In the above example the codec variable is only visible within the function it is defined in, i.e. the module's scope function. So, anything defined with var will be a private module object. To make objects visible outside of the module the mod object is used. As mentioned earlier the mod object represents the public interface of the module. Any property added to this object will become a public module object. In the code above the testCodecs() for example is a public method of the tutorial module. To use that method all one needs to do is import the totorial module and call testCode():

var tutorial = imprt('tutorial');
tutorial.testCodecs();

or even shorter:

imprt('tutorial').testCodecs();
try the code above

Thats it, we have now sucessfully created a module.
The next step is creating a jsolait class.