CoffeeScript的:如何返回数组从类? [英] CoffeeScript: How to return a array From class?

查看:116
本文介绍了CoffeeScript的:如何返回数组从类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是错在这个类中的CoffeeScript ??

  @moduleEuclidean2D - >
  类@Point
    构造器:(X,Y) - GT;
      返回如果Float32Array?然后Float32Array([X,Y])其他阵列(X,Y)

我希望它表现得像个:

  P =新的点(1.0,2.0);
P [0] == 1.0
P [1] == 2.0

但随着茉莉花测试我得到预期不确定等于1。

 形容点 - >    beforeEach  - >
      @point =新Euclidean2D.Point(1.0,2.0)    它中提取值 - >
      (期望@point [0])。toEqual 1.0
      (期望@point [1])。toEqual 2.0

有没有的CoffeeScript或茉莉花??

错误

此外这一切就像一个模块中:

  @module =(姓名,FN) -  GT;
  名字= names.split'。如果typeof运算名字是字符串
  空间= @ [names.shift()] || = {}
  space.module || = @module
  如果names.length
    space.module名字,FN
  其他
    fn.call空间

在Chrome的控制台我得到:

  A =新Euclidean2D.Point(1.0,2.0)
- >点
一个[0]
未定义
B =新Float32Array([1.0,2.0])
- > Float32Array
B [0]
1

编辑:后,再次..对不起

具有使用@brandizzi和@ arnaud576875答案的组合解决。在官方的CoffeeScript维基prposed的@module没有工作。其结果是:

 类@Point
        构造器:(X,Y) - GT;
            返回如果Float32Array?然后Float32Array([X,Y])其他阵列(X,Y)


解决方案

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

  P =新Euclidean2D.Point(1.0,2.0)

如果您想从构造函数返回一个数组,这样做明确的:

 构造器:(X,Y) -  GT;
  返回如果Float32Array?然后Float32Array([X,Y])其他阵列(X,Y)

(默认情况下,CoffeeScript的不从构造函数返回值,所以你必须明确地去做。)


您可以这样做,太:

 类@Point
  构造器:(X,Y) - GT;
    @ [0] = X
    @ [1] = 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)

解决方案

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    

这篇关于CoffeeScript的:如何返回数组从类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆