Coffeescript:处理实例类方法的更好方法
我正在尝试从嵌套对象(“操作”)内访问实例变量。我能找到的唯一解决方法是使用本地 var ('_') 来表示父对象。
Class Mover
...
Constructor () =>
_ = this
@mode = "wave"
@action= {
wave: ->
_.x = _.ox
_.y = _.oy = Math.cos(window.G.time * _.speed + _.c + _.vary) * _.amp - _.amp * .5
return
jump: ->
_.y = _.oy = Math.min(0,Math.cos(window.G.time*_.speed + _.c * _.vary)) * _.amp - _.amp * .5
_.x = _.ox
return
@loop =>
@action[@mode]()
I'm trying to access instance variables from within a nested object ('action'). The only workaround I could find was using a local var ('_') to represent the parent object.
Class Mover
...
Constructor () =>
_ = this
@mode = "wave"
@action= {
wave: ->
_.x = _.ox
_.y = _.oy = Math.cos(window.G.time * _.speed + _.c + _.vary) * _.amp - _.amp * .5
return
jump: ->
_.y = _.oy = Math.min(0,Math.cos(window.G.time*_.speed + _.c * _.vary)) * _.amp - _.amp * .5
_.x = _.ox
return
@loop =>
@action[@mode]()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在操作对象内使用粗箭头 (=>),如下所示:
Try using a fat arrow (=>) inside your action object like so: