BookmarkSubscribeRSS Feed
Craige_Hales

Staff

Joined:

Mar 21, 2013

Write reusable JSL

Problem

You have JSL that can be used in more than one project, with minor changes, and you want to keep one copy to make it easier to maintain.

Solution

Use a separate file and include it into a main program. Use namespaces to keep variables isolated from other modules. Build the namespaces so the main program can add customizations.

// "example.jsl" reusable, extensible module

example = New Namespace( "example" ); // namespace to hold the reusable code

example:aaa = 1; // a variable in the namespace

// this represents the extensible part of the module
example:uuu = Function( {p}, Return( p ) ); // no transform by default

// this represents the reusable part of the module
example:fff = Function( {p}, // do something amazing
    example:aaa += 1;
    Return( example:uuu( p ) * example:aaa );
);
// "mainprog.jsl" uses the example code, and extends it

Include( "example.jsl" );

// this function replaces the default
example:uuu = Function( {p},
    Return( Sqrt( p ) )
);

// use the amazing code from the package
p = example:fff( 16 );
q = example:fff( 9 );
r = example:fff( 4 );
Show( p, q, r );

Discussion

You might want to add an example:init( parameters ) function that you call after the include to do one time setup. Or, if there are no parameters, you can do the one time setup in the example.jsl file.

See Also

Scripting index describes several additional parameters.

Article Labels
Article Tags
Contributors