問題
您有可以在多個項目中使用的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文件中進行一次性設置。
也可以看看
腳本索引描述了幾個附加參數。