简体   繁体   中英

How to pass arguments to a “sub class” in coffeescript

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

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.

PS 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM