Coffeescript how to use extensions for an array

I am trying to use extensions for an array. What should I add to the constructor. Here is the code

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

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

It doesn't seem to work.

+5
source share
2 answers

There is no easy way to "inherit" from a prototype array. You must 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 on embedded complex solutions that will fail as soon as you try to parse the array or access its length value.

more about this problem:

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

+5
source

I haven't used a coffee script lately, so this is just a hunch, but it could be something like:

class List extends [].prototype

can work?

: "mu is too short", , script. , SO , : "" javascript?

-1

All Articles