问题
您有可以在多个项目中使用的JSL,只需稍作更改,并且您希望保留一个副本以使其更易于维护。
解
使用单独的文件并将其包含在主程序中。使用命名空间来保持变量与其他模块隔离。构建命名空间,以便主程序可以添加自定义。
// "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 );
讨论
您可能想要添加一个示例:init(参数) 在include之后调用的函数进行一次设置。或者,如果没有参数,您可以在example.jsl文件中进行一次性设置。
也可以看看
脚本索引描述了几个附加参数。