gpt4 book ai didi

javascript - coffeescript 如何使用数组的扩展

转载 作者:搜寻专家 更新时间:2023-11-01 04:19:35 25 4
gpt4 key购买 nike

我正在尝试使用数组的扩展..我应该在构造函数中放入什么..这是代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com