What are the diferences between these field declarations

50 Views Asked by At

In MoonScript, what is the difference between these types field declarations?:

class SomeClass
     a : {}
    @b : {}
     c = {}
    @d = {}
1

There are 1 best solutions below

3
On

a: {} creates a 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: {} creates b 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. With self 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.