CoffeeScript:如何从类返回数组?

发布于 2024-12-05 01:21:37 字数 1327 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2024-12-12 01:21:37

您应该使用 new 来实例化对象:

p = new Euclidean2D.Point(1.0,2.0)

如果您想从构造函数返回一个数组,请明确执行:(

constructor: (x,y) -> 
  return if Float32Array? then Float32Array([x,y]) else Array(x,y)

默认情况下,Coffeescript 不会从构造函数返回值,因此您必须这样做明确地。)


你也可以这样做:

class @Point
  constructor: (x,y) ->
    @[0] = x
    @[1] = y    

You should use new to instantiate the object:

p = new Euclidean2D.Point(1.0,2.0)

If you want to return an Array from the constructor, do it explicitly:

constructor: (x,y) -> 
  return if Float32Array? then Float32Array([x,y]) else Array(x,y)

(By default, Coffeescript doesn't return values from the constructor, so you have to do it explicitly.)


You could have done that, too:

class @Point
  constructor: (x,y) ->
    @[0] = x
    @[1] = y    
疑心病 2024-12-12 01:21:37

您正在定义一个构造函数,但期望它的行为类似于函数。然而,构造函数只是在要返回的对象中设置值。由于构造函数没有在初始化对象中设置任何属性,因此它确实没有用。

您有一些替代方案:

  1. 将类初始化为@amaud sugested。

  2. 以 @amaud 建议的方式从构造函数返回值(这对我来说没有多大意义。这不是我感觉的构造函数的功能。在这种情况下,解决方案 #3 似乎更好)。

  3. 定义一个函数而不是一个类。恕我直言,是最简单、最实用的解决方案

    @Point = (x, y) ->
        如果是Float32Array?然后 Float32Array([x,y]) else Array(x,y)
    
  4. 如果您希望 Point 成为 Float32ArrayArray 的特化,请使用以下选项#1 但让 Point 从您想要的类继承:

    超类 = 如果是 Float32Array?然后 Float32Array else Array  
    
    类 @Point 扩展超类
      构造函数:(x,y) ->
        @[0] = x
        @[1] = y
    

编辑:@amaud676875 作为评论发布了一个有趣的问题。由于合理的答案会涉及一些代码,因此我将答案作为编辑发布。

@amaud,为了验证你的观点,我编写了以下 CoffeeScript 模块:

class Float32Array extends Array
  first: -> # Just for testing
    @[0]


superclass = if Float32Array? then Float32Array else Array

class @Point extends superclass
  constructor: (x,y) ->
    @[0] = x
    @[1] = y

然后我在控制台中导入了该模块:

coffee> point = require './point'
{ Point: { [Function: Point] __super__: [ constructor: [Object], first: [Function] ] },
 Float32Array: { [Function: Float32Array] __super__: [] } }

并创建了一个 Point

 coffee> p = new point.Point 3, 2
 [ 3, 2 ]

这个 Point 具有 来自 Float32Array 的 first() 方法:

 coffee> p.first()
 3

并且 instanceof 也表示它是 Float32Array 的实例:

coffee> p instanceof point.Float32Array
true

所以我打赌 新点 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:

  1. Initialize the class as @amaud sugested.

  2. 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).

  3. define a function instead of a class. IMHO, is the most simple and functional solution

    @Point = (x, y) ->
        if Float32Array? then Float32Array([x,y]) else Array(x,y)
    
  4. If you want Point to be either a specialization of Float32Array or Array, use the option #1 but make Point to inherit from the class you want:

    superclass = if Float32Array? then Float32Array else Array  
    
    class @Point extends superclass
      constructor: (x,y) ->
        @[0] = x
        @[1] = y
    

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:

class Float32Array extends Array
  first: -> # Just for testing
    @[0]


superclass = if Float32Array? then Float32Array else Array

class @Point extends superclass
  constructor: (x,y) ->
    @[0] = x
    @[1] = y

Then I imported the module in the console:

coffee> point = require './point'
{ Point: { [Function: Point] __super__: [ constructor: [Object], first: [Function] ] },
 Float32Array: { [Function: Float32Array] __super__: [] } }

and created a Point:

 coffee> p = new point.Point 3, 2
 [ 3, 2 ]

This Point has the first() method from Float32Array:

 coffee> p.first()
 3

and instanceof says it is an instance of Float32Array, too:

coffee> p instanceof point.Float32Array
true

So I bet new Point x, y returns an instance of Float32Array. Of course it is an instance of Point, too, and it is not a problem because Point is-a Float32Array, to use a classical OOP expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文