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

前端 未结 4 1892
深忆病人
深忆病人 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:08

    Array.length is a special property that doesn't work when subclassed.

    If you look at the compiled javascript for your first example the way CoffeeScript builds classes is by creating a function and calling __extends(SetPositionsArray, Array);. This method can't 'copy' the concept of length into your new class.

提交回复
热议问题