CoffeeScript:如何从类返回数组?
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)
What is wrong in this class in CoffeeScript ??
@module "Euclidean2D", ->
class @Point
constructor: (x,y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)
I want it to behave like:
p = new Point(1.0,2.0);
p[0] == 1.0
p[1] == 2.0
But testing with Jasmine I get "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 there an error in CoffeeScript or in Jasmine ??
Also all of it is in a module like:
@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 the Chrome Console I get:
a = new Euclidean2D.Point(1.0,2.0)
-> Point
a[0]
undefined
b = new Float32Array([1.0,2.0])
-> Float32Array
b[0]
1
EDIT: , again.. sorry
Has solved using an combination of @brandizzi and @arnaud576875 answers. The @module prposed in the official CoffeeScript Wiki did not work. The result is:
class @Point
constructor: (x, y) ->
return if Float32Array? then Float32Array([ x, y ]) else Array(x,y)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 new 来实例化对象:
如果您想从构造函数返回一个数组,请明确执行:(
默认情况下,Coffeescript 不会从构造函数返回值,因此您必须这样做明确地。)
你也可以这样做:
You should use
new
to instantiate the object:If you want to return an Array from the constructor, do it explicitly:
(By default, Coffeescript doesn't return values from the constructor, so you have to do it explicitly.)
You could have done that, too:
您正在定义一个构造函数,但期望它的行为类似于函数。然而,构造函数只是在要返回的对象中设置值。由于构造函数没有在初始化对象中设置任何属性,因此它确实没有用。
您有一些替代方案:
将类初始化为@amaud sugested。
以 @amaud 建议的方式从构造函数返回值(这对我来说没有多大意义。这不是我感觉的构造函数的功能。在这种情况下,解决方案 #3 似乎更好)。
定义一个函数而不是一个类。恕我直言,是最简单、最实用的解决方案
如果您希望
Point
成为Float32Array
或Array
的特化,请使用以下选项#1 但让Point
从您想要的类继承:编辑:@amaud676875 作为评论发布了一个有趣的问题。由于合理的答案会涉及一些代码,因此我将答案作为编辑发布。
@amaud,为了验证你的观点,我编写了以下 CoffeeScript 模块:
然后我在控制台中导入了该模块:
并创建了一个
Point
:这个
Point
具有来自
方法:Float32Array
的 first()并且
instanceof
也表示它是Float32Array
的实例:所以我打赌
新点 x, y
返回 Float32Array 的实例。当然它也是一个 Point 的实例,这不是问题,因为 Point 是一个 Float32Array >,使用经典的 OOP 表达式。You are defining a constructor but expecting that it behaves like a function. The constructor, however, just sets values in the object to be returned. Since your constructor does not set any attributes in the initializing object, it really does not useful.
You have some alternatives:
Initialize the class as @amaud sugested.
Returns the value from the constructor as @amaud sugested (which does not make much sense to me. This is not the function of a constructor as I feel it. In this case the solution #3 seems better).
define a function instead of a class. IMHO, is the most simple and functional solution
If you want
Point
to be either a specialization ofFloat32Array
orArray
, use the option #1 but makePoint
to inherit from the class you want:EDIT: @amaud676875 posted an interesting question as a comment. Since a reasonable answer would involve some code, I am posting the answer as a edit.
@amaud, for verifying your point, I wrote the following CoffeeScript module:
Then I imported the module in the console:
and created a
Point
:This
Point
has thefirst()
method fromFloat32Array
:and
instanceof
says it is an instance ofFloat32Array
, too:So I bet
new Point x, y
returns an instance ofFloat32Array
. Of course it is an instance ofPoint
, too, and it is not a problem becausePoint
is-aFloat32Array
, to use a classical OOP expression.