Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript how to use extends for Array

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

like image 225
coool Avatar asked Oct 09 '22 09:10

coool


1 Answers

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/

like image 75
mpm Avatar answered Oct 12 '22 10:10

mpm