Created
December 21, 2012 19:00
-
-
Save kossnocorp/4354995 to your computer and use it in GitHub Desktop.
Classical JS pitfall
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Amplifr.BaseView extends Backbone.View | |
| constructor: -> | |
| @createSelectors() | |
| super | |
| els: {} | |
| createSelectors: -> | |
| for name, selector of @els | |
| cachedMatches = selector.match(/^cached (.+)/) | |
| @['$' + name] = => | |
| if cachedMatches | |
| @['$_' + name] ||= @$(cachedMatches[1]) | |
| else | |
| @$(selector) |
Author
Coffeescript.
Author
@private-face nice try, but wrong.
Ставлю на то, что при вызове @"$_'" + name этот самый name внутри функции будет уже не тот, что надо. Кому-то нужно еще одно замыкание и я солидарен с @private-face, использование coffeescript само по себе критичный баг ;)
А что хранится в @els? Можно примерчик?
@itrelease, просто javascript вообще не нужен. В топку его!
@whitequark программирование говно!
Author
@itrelease ага, cachedMatches будет из последнего вызова.
createSelectors: ->
for name, selector of @els
do (name, selector) =>
cachedMatches = selector.match(/^cached (.+)/)
@['$' + name] = =>
if cachedMatches
@['$_' + name] ||= @$(cachedMatches[1])
else
@$(selector)
Author
@milushov примерно вот так:
els:
new: '@new'
link: '@link'
tabs: '@tab'
input: 'cached @input'@new, @link это алиас на [role='new'].
Author
@whitequark @itrelease и компьютеры отстой.
for is probably the worst part of Coffee
Author
@inossidabile maybe worst part of JS?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code contain critical bug. What kind is bug?