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 # 1use
collectionobject holder of elements (like e.g. jquery it). loose native array features, though. might subclassarrayextent (.lengthnot update automatically).class collection constructor: (col = []) -> @length = 0 el in col array::push.call(@, el) obj[0] # 1
Comments
Post a Comment