我一直在进行一些试验,并且遇到过几次通过循环应用 CS“类”似乎是有意义的情况。
例如:
if ($areas = $('.itemParent')).length >= 1
    class SomeClass
        constructor: (el) ->
            @$parent = el
            @$overflow = el.find('.overflow')
            @$items = @$overflow.find('.item')
            @max = @$items.length - 1
            @current = 0
        # Gets us to an area
        goToItem: (i) ->
            @$overflow.animate
                scrollLeft: @$items.eq(i).position().left
            , 450, 'easeInOutQuad'
        # Binds each item
        bindItems: ->
            @$parent.find('.item').bind 'click tap', (e) =>
                el = $(e.target).parents('.item')
                @$items.removeClass('active')
                el.addClass('active')
                @goToItem(el.index())
            @
    # Iterate and apply the structure to all areas
    $areas.each -> 
        area = new SomeClass($(@))
        area.bindItems()
它似乎比“全局”绑定它们更有条理。这是一种糟糕的方法吗?