coffeescript - dynamically adding members to coffee-script object -


so have object looks

module.exports = class book extends events.eventemitter      constructor: ->          @books =              a: new small_book('aaa')             b: new small_book('bbb')             c: new small_book('ccc')             d: new small_book('ddd')      update: (pair, callback) ->         @books[pair].update_book()         @emit 'update' 

however, doing this,

 pairs =       a: 'aaa'      b: 'bbb'      c: 'ccc'      d: 'ddd'   module.exports = class book extends events.eventemitter       constructor: ->                each pair in pairs              @books[pair] = new small_book(pairs[pair]) 

or going through list , adding many pairs in list. how can this?

from fine manual:

comprehensions can used iterate on keys , values in object. use of signal comprehension on properties of object instead of values in array.

yearsold = max: 10, ida: 9, tim: 11  ages = child, age of yearsold   "#{child} #{age}" 

so if want loop on object, this:

constructor: ->     @books = { }            k, v of pairs          @books[k] = new small_book(v) 

demo: http://jsfiddle.net/ambiguous/gnvxa/


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -