admin管理员组文章数量:1432390
I am using _.bindAll
in lots of my Backbone.Views.
_.bindAll(this, 'render', 'addOne', 'addAll', 'someFunctionA', 'someFunctionB');
While refactoring this bees quite tedious as I need to keep the views methods and the name listings in sync. Both ways this often leads to simple errors.
As there is a short version of bindAll
, which would eliminate this need, I am wondering what drawbacks (performance, readability, flexibility, ..) do exist and do you consider them acceptable to gain a bit of a productivity boost.
_.bindAll(this);
I am using _.bindAll
in lots of my Backbone.Views.
_.bindAll(this, 'render', 'addOne', 'addAll', 'someFunctionA', 'someFunctionB');
While refactoring this bees quite tedious as I need to keep the views methods and the name listings in sync. Both ways this often leads to simple errors.
As there is a short version of bindAll
, which would eliminate this need, I am wondering what drawbacks (performance, readability, flexibility, ..) do exist and do you consider them acceptable to gain a bit of a productivity boost.
_.bindAll(this);
Share
Improve this question
edited Feb 7, 2012 at 18:16
SunnyRed
asked Feb 7, 2012 at 11:37
SunnyRedSunnyRed
3,5554 gold badges37 silver badges58 bronze badges
2 Answers
Reset to default 4There is no practical performance penalty for using that form of bindAll. However, it will be a pain if you do not want a method bound to this
for some reason.
However, you may find that you do not need to use bindAll as often as you think. All methods that are bound to event handlers (with the events hash) are automatically bound to this
.
Also, when you are explicitly binding an event, you can pass the this
binding in the third param. For example:
this.model.bind('change', this.render, this)
I'v used _.bindAll(this)
in backbone project for a while. After profiling the code I'v realized that calls to _.bindAll
takes notable part of all the function calls.
Based on the results of the performance test http://jsperf./underscore-bindall-this-vs-bindall-this-params
it seems that explicit method naming is more performant (especially for objects with a lot of functions)
本文标签: javascriptUnderscore bindAllexplicit method namingStack Overflow
版权声明:本文标题:javascript - Underscore bindAll, explicit method naming - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744603040a2615182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论