Why does setting positions in a subclass of Array not change its length? Should I not subclass array?

前端 未结 4 1893
深忆病人
深忆病人 2021-01-19 04:56

In the CoffeeScript program below, I create a subclass of Array which sets two positions in its constructor:

class SetPositionsArray extends Arr         


        
4条回答
  •  借酒劲吻你
    2021-01-19 05:30

    I realize I'm rather late in the game here, but you shouldn't need to do what you're doing. Here's an example I've been tinkering with and I see no reason why it shouldn't work for you.

    class ArrayWrapper
      constructor: (name, args...)
        @name = name
        @push.apply(@, args)
    

    Once this is in place I can do this:

    >>> relation = new Relation('foo',1,2,3,4,5)
    Object[1, 2, 3, 4, 5]
    >>> relation.length
    5
    

提交回复
热议问题