by sync · 14y ago · 0 comments
That being said, some of their examples are lackluster.
@originalStyle = {}
for k in ['top', 'left', 'width', 'height']
@originalStyle[k] = @element.style[k]
Should really be something like: @originalStyle = ['top', 'left', 'width', 'height'].reduce (hash, position) ->
hash[position] = @element.style[position]
hash
, {}
... though that shows off some CoffeeScript warts.Also,
Sharing =
init: (sf_info) ->
for list in [sf_info.current, sf_info.past]
for info in list
@_decode_sort_key info
Why aren't they using CoffeeScript classes? class Sharing
constructor: (sfInfo) ->
...By doing it this way, you've introduced another symbol for us to consider (hash) and by departing from the standard for loop format, you've made it less idiomatic, which means more people will have to expend mental energy parsing it.
@originalStyle = {k : @element.style[k] for k in ['top', 'left', 'width', 'height'}
Unfortunately this syntax was proposed and rejected [1]. But with a simple to_hash library function, you can achieve something similar [2]: to_hash = (pairs) ->
hash = {}
hash[key] = value for [key, value] in pairs
hash
@originalStyle = to_hash ([k, @element.style[k]]
for k in ['top', 'left', 'width', 'height'])
[1] https://github.com/jashkenas/coffee-script/issues/467 @originalStyle = _(@element.style).pick ['top', 'left', 'width', 'height'] original_style = element.style.only('top', 'left', 'width', 'height')
Or if you do not want to rely on ActiveSupport: original_style = Hash[%w(top left width height).map{ |p| [p, element.style[p]] }] %w(top left width height).reduce({}) { |memo, pos| memo[position] = @element.style[position] and memo }