Visibility of a javaScript class
How to make a class defined in one file visible in another file? Using javaScript language support not MicroScript.
export function does not work
file: exporting/exp1
class abc {
constructor() {
print("construct abc")
}
test(t, e, n) {
print("called test")
}
}
// export keyword is unknown -> results in exception, but abc in main is undefined
export {abc as D};
main:
init = function()
{
//does not work
print(adc)
print(D)
}
Console:
Unexpected token 'export', in file "exporting/exp1" at line 34
https://microstudio.io/i/kesha/jsclassvisibility/
Missing any information about scopes of files and variables. According to JavaScript documentation all variables that defined outside functions/classes are global. This is not the case here.
Workaround: use the global window object to store what you need and use it in another file
If I understood correctly what you want to do, then you need to:
- Replace
class abc{
with abc = class{
- and to access it, make a new instance of the class
example = new abc(); print(example)
The keywords "import" and "export" do not work in MicroScript because of how the code you have created is added to the existing code.
This limitation is on the JavaScript side because it does not allow the words "import" and "export" to be used in conjunction with the "eval" command.
Like @Loginus said, the way JavaScript is run in MicroStudio is not modular. As far as I can tell, all the code is combined into one string that is evaluated. Like @Romero said, class Abc { /* ... */ }
will define the class locally. If you want to use modular JavaScript, it's possible to do so with assets.
@All thanks for replies. Using JavaScript (Options->Javascript support enabled) not microScript.
It looks like only variables and functions are saved globally but not classes. The hint saving the class in a variable works:
file 1:
class abc {
constructor() {
print("construct abc")
}
test() {
print("called test")
}
}
abcClass = abc;
function abcde ()
{
print("abcde")
}
main:
init()
{
const a = new abcClass();
abcde();
}
output:
construct abc
abcde
@TwiceUponATime how to do that? there are no suitable functions in asset_manager. example would be cool