我正在尝试使用数组的扩展..我应该在构造函数中放入什么..这是代码
class List extends Array
constructor: ()->
super arguments
this
list = new List("hello","world")
alert list[0]
好像不行..
最佳答案
没有简单的方法可以从数组原型(prototype)“继承”。你应该使用 composition ,即
class List
constructor: ()->
this.array = new Array(arguments);
getArray :()->
this.array
list = new List("hello","world")
alert list.getArray()[0]
否则您将花时间实现复杂的解决方案,一旦您尝试解析数组或访问其长度值,这些解决方案就会失败。
关于这个问题的更多信息:
http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
关于javascript - coffeescript 如何使用数组的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688009/