javascript - foreach loop with link to 'this' in CoffeeScript -


there following code:

  user.prototype.convertfrompermissionstoscopes = ->     this.scopes = {}     scopesnames = ['create', 'delete', 'update', 'show']     groupname of this.permissions       this.scopes[groupname] = {}       scopesnames.foreach (scopename) ->         this.scopes[groupname][scopename] = this.permissions[groupname].indexof(scopename) isnt -1 

i got error 'this.scopes undefined' @ last line. how can fix it? thanks!

use fat arrow pass outer this context of foreach:

scopesnames.foreach (scopename) => 

that ensure outer scope passed context of method.


just sidenote can use :: prototype , @ this:

user::convertfrompermissionstoscopes = ->   @scopes = {}   scopesnames = ['create', 'delete', 'update', 'show']   groupname of @permissions     @scopes[groupname] = {}     scopesnames.foreach (scopename) =>       @scopes[groupname][scopename] = @permissions[groupname].indexof(scopename) isnt -1 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -