【发布时间】:2011-11-17 07:38:11
【问题描述】:
CoffeeScript 中的这个类有什么问题??
@module "Euclidean2D", ->
class @Point
constructor: (x,y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)
我希望它表现得像:
p = new Point(1.0,2.0);
p[0] == 1.0
p[1] == 2.0
但是用 Jasmine 测试我得到“预期未定义等于 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
CoffeeScript 或 Jasmine 是否有错误??
所有这些都在一个模块中,例如:
@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
在 Chrome 控制台中我得到:
a = new Euclidean2D.Point(1.0,2.0)
-> Point
a[0]
undefined
b = new Float32Array([1.0,2.0])
-> Float32Array
b[0]
1
编辑:,再次......对不起
结合使用@brandizzi 和@arnaud576875 答案已解决。官方 CoffeeScript Wiki 中提出的 @module 不起作用。结果是:
class @Point
constructor: (x, y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)
【问题讨论】:
标签: javascript arrays indexing coffeescript