如何将参数传递给“子类”在咖啡脚本中

发布于 2025-01-07 00:51:23 字数 942 浏览 1 评论 0原文

我有以下代码,它在咖啡脚本中定义了迭代器原型(扩展数组原型):

App.Utils.Iterator = do ->
  Iterator =  ->
    Array.apply(this,arguments)
    ### this.push(i) for i in arguments : works but not good practice
    iter = 0
    @reset = ->
      iter = 0
    @getIndex = ->
      iter
    @getValue= ->
      this[iter]
    @next= ->
      this[++iter] if iter < this.length-1
    @previous= ->
      this[--iter] if iter>0
    @hasNext= ->
      return if this[iter+1] then true else return false
    @hasPrevious= ->
      return  if this[iter-1]then true else return false
    return this

  Iterator.prototype = new Array()
  Iterator.prototype.constructor = Array
  return Iterator

一切正常,但是我想像数组一样实例化迭代器:

iterator = new App.Utils.Iterator(1,2,3,4)

它在控制台上返回 []

如何让它像常规迭代器一样初始化我的迭代器数组( new Array(1,2,3,4) 返回 [1,2,3,4] )而不将参数推送到构造函数中(该指令已在我的代码中注释)

谢谢

I have the following code which defines an Iterator prototype in coffeescript ( extending the Array prototype ) :

App.Utils.Iterator = do ->
  Iterator =  ->
    Array.apply(this,arguments)
    ### this.push(i) for i in arguments : works but not good practice
    iter = 0
    @reset = ->
      iter = 0
    @getIndex = ->
      iter
    @getValue= ->
      this[iter]
    @next= ->
      this[++iter] if iter < this.length-1
    @previous= ->
      this[--iter] if iter>0
    @hasNext= ->
      return if this[iter+1] then true else return false
    @hasPrevious= ->
      return  if this[iter-1]then true else return false
    return this

  Iterator.prototype = new Array()
  Iterator.prototype.constructor = Array
  return Iterator

Everything works fine however i want to instanciate the iterator like an Array :

iterator = new App.Utils.Iterator(1,2,3,4)

which returns [] on the console

how to make it initialize my iterator like a regular array ( new Array(1,2,3,4) returns [1,2,3,4] ) without pushing the arguments in the constructor function ( the instruction has been commented in my code )

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-01-14 00:51:23

您的代码中存在多个设计错误,因此您尝试创建的实体不是迭代器。迭代器是用来迭代现有数组或其他可迭代数据类型的东西 - 它不是可迭代本身。这就是为什么:

  1. 构造函数的合理参数是数组 (new Iterator [1,2,3,4])
  2. Iterator 不应扩展 Array
  3. 不用重置迭代器,只需为每次迭代创建一个新实例

现在回答你的问题。

首先,无需重新创建一堆 Javascript 样板,您的代码就可以重写为:

class Iterator extends Array
  constructor: ->
    iter = 0
    @reset = ->
      iter = 0
    @getIndex = ->
      iter
    @getValue = ->
      @[iter]
    @next = ->
      @[++iter] if iter < @length-1
    @previous = ->
      @[--iter] if iter > 0
    @hasNext = ->
      if @[iter+1] then true else false
    @hasPrevious = ->
      if @[iter-1] then true else false
    super

其次,为什么要让这样的事情过于复杂并仅仅为了在动态命令式语言中“隐藏”一个变量而引入开销?代码必须重写为以下形式:

class Iterator extends Array
  constructor: ->
    @iter = 0
    super
  reset: ->
    @iter = 0
  getIndex: ->
    @[@iter]
  # ...

第三,不可能将参数列表传递给扩展数组。可能是与糖相关的 javascript 问题。您在构造函数解决方案中推送参数的提议很好。考虑到如果您仍然坚持编写这样的课程,您将犯下的所有其他错误,我认为良好实践的问题无论如何都不应该困扰您。

PS 为什么有人需要 Coffeescript 中的迭代器是一个问题。我认为在深入编写一个你不会使用的库之前,你最好阅读一些有关 Coffeescript 的书。尝试这个

There are several design mistakes in your code because of which the entity you're trying to create is anything but an Iterator. Iterator is something you use to iterate over an existing array or other iterable data type - it is not the iterable itself. That's why:

  1. a reasonable parameter to your constructor is an array (new Iterator [1,2,3,4])
  2. Iterator should not extend Array
  3. instead of resetting iterator just create a new instance of it for every iteration

Now to your question.

Firstly, without recreating a bunch of Javascript boilerplate your code could be rewritten as:

class Iterator extends Array
  constructor: ->
    iter = 0
    @reset = ->
      iter = 0
    @getIndex = ->
      iter
    @getValue = ->
      @[iter]
    @next = ->
      @[++iter] if iter < @length-1
    @previous = ->
      @[--iter] if iter > 0
    @hasNext = ->
      if @[iter+1] then true else false
    @hasPrevious = ->
      if @[iter-1] then true else false
    super

Secondly, why overcomplicate things like that and introduce overhead just for the sake of "hiding" one variable in a dynamic imperative language? The code must be rewritten in the following form:

class Iterator extends Array
  constructor: ->
    @iter = 0
    super
  reset: ->
    @iter = 0
  getIndex: ->
    @[@iter]
  # ...

Thirdly, it is impossible to pass a list of parameters to an extended array. Probably it is a [] sugar-related javascript issue. The proposed by you pushing the arguments in the constructor solution is fine. Considering all the other mistakes you'll make if you'll still stick to writing such a class I don't think that question of good practice should bother you anyway.

P.S. Why anybody would need an iterator in Coffeescript is a question. I think you should better read some book about Coffeescript before diving into programming a library which you won't use. Try this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文