org/mathdox/javascript/core.js documentation

Defines the core javascript functions that allow you to import javascript files, create packages and extend objects.

$require and $main

The $require and $main functions allow you to specify that certain javascript files need to be loaded before a main section of code is executed.

Example of $require and $main usage:

        $require("foo.js");
        $require("bar.js");

        $main(function(){

          // here you can be sure that both foo.js and bar.js are loaded

        });
        

When the javascript file that you want to load (for instance, the baz.js from the example below) doesn't contain a call to $main, then the system can not detect whether or not this file has been loaded properly. In this case, you need to specify a second argument to the $require function. This second argument is a function that should return true whenever the file has loaded. Typically, such a function would test for the presence of a variable that is set by the javascript file.

Example of $require with two arguments:

        $require("foo.js");
        $require("bar.js");
        $require("baz.js", function() { return VariableSetByBaz != null; });

        $main(function(){

          // here you can be sure that foo.js, bar.js and baz.js are loaded

        });
        

$package

Although javascript doesn't have a package system, it is customary to use objects to simulate packages. To facilitate this, the $package function allows you to quickly reference and create package objects.

Example of $package usage:

        $package("com.foo");

        // now the com.foo package object is guaranteed to exist, so we can
        // create objects in it
        com.foo.SomeObject = {

          // attributes and methods go here

        }
        

The $package function will only create a new package object when it doesn't already exist, so it is safe to call it more than once. Because the $package function also returns the package object it is possible to shorten the example above to the following:

Shorter example:

        $package("com.foo").SomeObject = {

          // attributes and methods go here

        }
        

$extend

In javascript constructor functions can be used to simulate classes. The $extend function is meant to facilitate the creation of such class constructors.

Example of $extend usage:

        var ChildClass = $extend(ParentClass, {

          // methods and attributes of the child class go here

        });
        

The first argument of the $extend function is the class that you wish to extend (the parent class), and the second argument is an object containing the added methods and attributes of the new class (the child class).

The $extend function can also be used to create a new class that doesn't need to inherit something from a parent class. In that case you can use the Object class as a parent.

Example of creating a class without a parent class:

        var SomeClass = $extend(Object, {

          // methods and attributes of the child class go here

        });
        

The first argument of the $extend function doesn't have to be a class constructor; it can also be a regular object.

Example of creating a class with a parent object:

        var NewMathClass = $extend(Math, {

          /**
           * Override the log method of the Math object so that it will take a
           * base argument.
           */
          log : function(a, base) {

            // check whether a base argument has been specified
            if (base != null) {

              // use the parent object's log method to calculate the log
              // with a base argument
              return (
                arguments.callee.parent.log(a) /
                arguments.callee.parent.log(base)
              );

            }
            else {

              // forward the function call to the parent object
              return arguments.callee.parent.log(a);

            }

          }

        });

        // use the new math class to calculate the 2 log of 64 :
        var newMath = new NewMathClass();
        alert( newMath.log(64,2) );
        

As you can see in the example above, you can still use overridden methods through the arguments.callee.parent object.

$baseurl

The $baseurl object is a string that contains the url from which the org/mathdox/javascript/core.js file was loaded. This can be used for instance to load images or other files that are closely related to the javascript source files.

Example of $baseurl usage:

        <script
          type='text/javascript'
          src='http://foo.com/bar/org/mathdox/javascript/core.js'
        ></script>

        <script type='text/javascript'>

          // shows the string 'http://foo.com/bar/baz/quux.png'
          alert( $baseurl + "baz/quux.png" );

        </script>
        

source code

view the source code