gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.31k stars 156 forks source link

bug?: set properties in constructor using the object setting parameter shorthand. #1130

Closed 1o0o0o0o1 closed 2 months ago

1o0o0o0o1 commented 2 months ago

In the constructor of class, if we set properties using the shorthand, there seems to be a bug.

Background

the shorthand syntax in the doc:

You can easily set properties in constructor functions and in methods using the object setting parameter shorthand.

and the example in the doc:

class A
  (@x) ->

  f: (@y) ->
    @x + @y

a = new A 2
a.x   #=> 2
a.f 3 #=> 5
a.y   #=> 3

test case: with shorthand syntax

If we firstly assign some values such as rows and columns, and then use these values later in the constructor, there is a bug.

class Test
    (@rows, @columns) ->
        @grid = do
          row <- Object.keys(Array(@rows).fill(0)) .map
          column <- Object.keys(Array(@columns).fill(0)) .map
          "#{row},#{column}"

t = new Test(3,3)
console.log t

the result is

{"rows":3,"columns":3,"grid":[["0,0"],["1,0"],["2,0"]]}

The grid is not what we expected.

test case: without shorthand syntax

class Test
    (rows, columns) ->
        @rows = rows
        @columns = columns
        @grid = do
          row <- Object.keys(Array(rows).fill(0)) .map
          column <- Object.keys(Array(columns).fill(0)) .map
          "#{row},#{column}"

t = new Test(3,3)
console.log(t)

the result is

{"rows":3,"columns":3,"grid":[["0,0","0,1","0,2"],["1,0","1,1","1,2"],["2,0","2,1","2,2"]]}

This works well.

I don't know if this condition is intended.

Update:

Sorry it is not a bug. There is a error in my code about the this bounds.