Skip to content

Need help understanding class member meanings #5453

@rayzorben

Description

@rayzorben

I noticed that you can put class members outside of the constructor in a class such as

class Test
    @foo = "foo" # static variable
     bar = "bar" # private variable
     baz: "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

class Test
	here: []
	test: 0

	constructor: (value, value2) ->
		@here.push value
		console.log @here
		console.log @test
		@test = value2
		console.log @test

testa = new Test 'a', 1
testb = new Test 'b', 2

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions