coo*_*ool 5 javascript coffeescript
我正在尝试使用extends for array ..我应该在构造函数中添加什么..这是代码
class List extends Array
constructor: ()->
super arguments
this
list = new List("hello","world")
alert list[0]
Run Code Online (Sandbox Code Playgroud)
似乎不起作用..
没有简单的方法从数组原型"继承".你应该使用组合,即
class List
constructor: ()->
this.array = new Array(arguments);
getArray :()->
this.array
list = new List("hello","world")
alert list.getArray()[0]
Run Code Online (Sandbox Code Playgroud)
或者您将花费时间实现复杂的解决方案,一旦您尝试解析数组或访问其长度值,这些解决方案就会失败.
更多关于这个问题:
http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
mac*_*ost -1
我最近没有使用咖啡脚本,所以这只是一个猜测,但也许是这样的:
class List extends [].prototype
Run Code Online (Sandbox Code Playgroud)
可能有用吗?
编辑:根据“mu 太短”的评论,问题似乎与咖啡脚本根本无关。您可能会发现此相关的 SO 帖子很有用,因为所选答案为该问题提供了两种可能的解决方案: 这是“子类化”javascript 数组的合理方法吗?
| 归档时间: |
|
| 查看次数: |
1837 次 |
| 最近记录: |