In MoonScript, what is the difference between these types field declarations?:
class SomeClass
a : {}
@b : {}
c = {}
@d = {}
In MoonScript, what is the difference between these types field declarations?:
class SomeClass
a : {}
@b : {}
c = {}
@d = {}
Copyright © 2021 Jogjafile Inc.
a: {}
createsa
in the base class and is available per instance.https://moonscript.org/reference/#the-language/table-literals
If the value is a table,
a: {}
will create it only once. To actually have unique tables, you will need to create it in the constructor (and thus set it in the instance): https://moonscript.org/reference/#the-language/object-oriented-programming@b: {}
createsb
in the class object and is "shared".https://moonscript.org/reference/#the-language/object-oriented-programming/class-variables
c = {}
is a class declaration statement, it is a local variable available during class declaration. Withself
you have access to the class.@d = {}
is a class declaration statement, but due to the@
prefix it's stored in the class.https://moonscript.org/reference/#the-language/object-oriented-programming/class-declaration-statements
Paste your code in https://moonscript.org/compiler/ to see all this in action.