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
Post a Comment