Array Access of Object Javascript -


in coffee script have little code snippet.

class collection    construct:(@collection=[]) 

now want access object if array want collection variable when so. in other languages implements arrayaccess of type , code next, current etc methods

obj= new collection([1,2,3])  obj[0] # should equal 1  

how can in javascript or coffeescript either do

that's impossible. need kind of proxy that. there no "arrayaccess" declaration feature, bracket notation property access on collection instance (like obj["collection"]).

instead, can:

  • implement getter function, like

    class collection   construct:(@collection=[])   at: (i) ->     @collection[i]  obj.at 0 # 1 
  • use collection object holder of elements (like e.g. jquery it). loose native array features, though. might subclass array extent (.length not update automatically).

    class collection   constructor: (col = []) ->     @length = 0     el in col       array::push.call(@, el)  obj[0] # 1 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -