You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that you can put class members outside of the constructor in a class such as
classTest@foo="foo"# static variablebar="bar"# private variablebaz:"baz"# instance variable accessible by @baz
The last one, the 'instance variable' seems to work, but has unexpected behavior when it comes to arrays. Other values are re-initialized across instantiations, but array variables are not
As you can see here, test always starts out at 0, but here doesn't get re-initialized and will keep getting new values pushed onto the old array, which I find odd.
Output:
[ 'a' ]
0
1
[ 'a', 'b' ]
0
2
The text was updated successfully, but these errors were encountered:
classTest@foo="foo"# static member (shorthand for `Test.foo = "foo"`)bar="bar"# class-scoped variable (behaves like a private static)baz:"baz"# instance member
With : in a class body you assign a prototype property. This is only done once.
Unless the value is a primitive, all class instances inherit the same object reference, which causes the behavior you see.
Default values for non-primitives should be assigned in the constructor (@member = value).
I noticed that you can put class members outside of the constructor in a class such as
The last one, the 'instance variable' seems to work, but has unexpected behavior when it comes to arrays. Other values are re-initialized across instantiations, but array variables are not
As you can see here,
test
always starts out at 0, buthere
doesn't get re-initialized and will keep getting new values pushed onto the old array, which I find odd.Output:
The text was updated successfully, but these errors were encountered: