Can you identify the issue in this CoffeeScript class?
@module "Euclidean2D", ->
class @Point
constructor: (x,y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)
I would like it to function as follows:
p = new Point(1.0,2.0);
p[0] == 1.0
p[1] == 2.0
However, when testing with Jasmine I am receiving an error message saying "Expected undefined to equal 1."
describe "Point", ->
beforeEach ->
@point = new Euclidean2D.Point(1.0,2.0)
it "extracts values", ->
(expect @point[0]).toEqual 1.0
(expect @point[1]).toEqual 2.0
Is the error related to CoffeeScript or Jasmine?
All of the above is wrapped within a module structure like so:
@module = (names, fn) ->
names = names.split '.' if typeof names is 'string'
space = @[names.shift()] ||= {}
space.module ||= @module
if names.length
space.module names, fn
else
fn.call space
In my Chrome Console output, I see:
a = new Euclidean2D.Point(1.0,2.0)
-> Point
a[0]
undefined
b = new Float32Array([1.0,2.0])
-> Float32Array
b[0]
1
EDIT: Apologies for the confusion.
The issue has been resolved by employing a combination of @brandizzi and @arnaud576875 answers. The @module suggested in the official CoffeeScript Wiki did not yield the desired outcome. The corrected code is:
class @Point
constructor: (x, y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)