doc/unity/en/knowjs/module.md
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.Eval("const a = 3");
////// much later
env.Eval("const a = function () {}");
}
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
int a = env.Eval<int>(@"
(function() {
const a = 3
return a;
})()
");
// a == 3
}
In fact, the Immediately Invoked Function Expression is a concept in JavaScript that is very similar to modules. It has an independent scope and can define its own output items, making it very convenient for encapsulating functionality.
Starting with the Immediately Invoked Function Expression, the JavaScript ecosystem has developed many module specifications, with the most popular being the official JS standard: ESM.
PuerTS supports executing modules that follow the ESM specification.
You can add a helloworld.mjs file to any Resources directory:
import { world } from 'lib.mjs'
console.log('hello ' + world);
Add a lib.mjs file to any Resources directory:
const world = 'puerts'
export { world }
Then import it using JsEnv.ExecuteModule.
void Start() {
Puerts.JsEnv env = new Puerts.JsEnv();
env.ExecuteModule("helloworld.mjs")
}
hello puertsin the consoleAfter writing JS in a separate file, the next step is to return to one of Puerts' focus areas: TypeScript (TS).