5

I am trying to use the extends for array ..what should I put in the constructor..Here is the code

class List extends Array
  constructor: ()->
      super arguments
      this

list = new List("hello","world")
alert list[0]

Doesn't seem to work..

coool
  • 8,085
  • 12
  • 60
  • 80

2 Answers2

5

There is no easy way to "inherit" from the array prototype. You should use composition , i.e.

    class List
      constructor: ()->
          this.array = new Array(arguments);
      getArray   :()->
          this.array


list = new List("hello","world")
alert list.getArray()[0]

or you will spend your time implemented complicated solutions that will fail as soon as you try to parse the array or access its length value.

more on the issue :

http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

mpm
  • 20,148
  • 7
  • 50
  • 55
-1

I haven't been using coffee script lately, so this is just a guess, but maybe something like:

class List extends [].prototype

might work?

EDIT: Per "mu is too short"'s comment, it seems the problem isn't coffee script related at all. You might find this related SO post useful, as the selected answer provides two possible solutions to this problem: Is this a reasonable way to 'subclass' a javascript array?

Community
  • 1
  • 1
machineghost
  • 33,529
  • 30
  • 159
  • 234