Using JavaScript Libraries
JavaScript libraries are blocks of code that another script can load
when it needs them. To load a library you use the Core.load()
function:
var lib = Core.load(Core.home + "Lib.js");
lib.f("param1", "param2"); // calls the 'f' function
in Lib.js
Library scripts are stored as text files and may contain any valid JavaScript
code. When JavaScript for OSA loads a library, it does the following:
- Reads the text file
- Compiles the library
- Executes the library
- Returns the result of the library script as the value of the Core.load()
function
In this way, JavaScript for OSA allows you to do pretty much anything
you like in libraries. The recommended approach is to have the script
library return an object containing a series of functions for the client
script to call.
Lib.js:
function func1(p1, p2) {
// do something with p1 and p2
}
var obj = {f:func1};
obj; // return the "loaded" object
|