ember-script icon indicating copy to clipboard operation
ember-script copied to clipboard

Static properties don't work

Open dynamix opened this issue 10 years ago • 3 comments

class Test
  @test = 1

causes a compiler error

Syntax error on line 2, column 2: unexpected '@' (\u0040)
1 : class Test
2 :   @test = 1

dynamix avatar Jul 16 '14 09:07 dynamix

Dynamix, you'll have to correct me if I'm wrong, but it looks like you should use the following syntax:

class Test
  test: 1

testing = new Test()
console.log(testing.test) # => 1

When declaring a class, we pass an object as a parameter (which can be hard to tell, with (coffee/ember)script's minimal syntax.

The above is equivalent to:

class Test ( { test: 1} )

Which sets the test property on the Test class, which you can then access with @test:

class Test
  test: 1
  testAccess: -> 
    console.log( @test )

testing = new Test()
testing.testAccess() # => 1

clov3r avatar May 05 '15 16:05 clov3r

Thanks for the response. However this is not the same as a static member in coffeescript (good explanation here : http://www.cs8.my/2012/09/coffeescript-classes-and-their-variables/). Seems like this is missing in coffeescript-redux.

dynamix avatar May 05 '15 16:05 dynamix

Oh yep, I hadn't seen that @var = "stuff" syntax before. Doing it with a colon also breaks. Bummer

clov3r avatar May 05 '15 17:05 clov3r