cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
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.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.