admin管理员组文章数量:1401158
I've been working in backbone.js and came across the following snippet of code.
_(view.buttonViews).each(function(button) {
button.render();
});
Where view.buttonViews is an array. If I take away the _() and have
view.buttonViews.each(function(button) {
button.render();
});
then I get an error that each is not a function. What does the _() add? Thanks!
I've been working in backbone.js and came across the following snippet of code.
_(view.buttonViews).each(function(button) {
button.render();
});
Where view.buttonViews is an array. If I take away the _() and have
view.buttonViews.each(function(button) {
button.render();
});
then I get an error that each is not a function. What does the _() add? Thanks!
Share Improve this question edited Oct 13, 2011 at 20:22 kinakuta 9,0371 gold badge40 silver badges48 bronze badges asked Oct 13, 2011 at 19:28 Phillip WhisenhuntPhillip Whisenhunt 1,3052 gold badges18 silver badges30 bronze badges 1- 1 _ is a valid identifier. In this case, _ is the name of a function. You could name a variable _, but in your example a library has created a function named _. – Jonathon Faust Commented Oct 13, 2011 at 19:31
4 Answers
Reset to default 11I guess it is the Underscore.js
library which provides the each
method:
_.each(list, iterator, [context])
Alias: forEach
Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is bound to the context object, if one is passed. Each invocation of iterator is called with three arguments: (element, index, list). If list is a JavaScript object, iterator's arguments will be (value, key, list). Delegates to the native forEach function if it exists.
This way, _([...]).each(...)
, is just another way of calling it.
BTW, it is also described in Backbone's documentation:
Backbone's only hard dependency is Underscore.js.
And FWIW, as @Jonathon already said, in general, _
is a valid variable name and in this case it contains a function. Adding parenthesis behind a function references calls that function and therefore, _()
calls the function referred to by _
. It is nothing special.
Besides that, parenthesis can occur as part of a function declaration or expression (function foo() {...}
) or as grouping operator (var i = (20 + 1) * 2;
).
Backbone is built on top of underscore, a utility library providing a lot of useful functionality that isn't native to JS but probably should be (eg things like traversing objects, array mapping, eliminating duplicate items in an array, that sort of thing).
It can be written using object-oriented or a functional style. So for instance your snippet of code could also be written thus:
_.each(view.buttonViews,function(button) {
button.render();
});
Backbone depends on Underscore which implements many utility functions. You can wrap an array with the _()
function and use the Underscore API as demonstrated here.
Underscore implements these functions without touching the prototype, so each
is not available to a regular array. However, it is callable from the object returned from the _
function, which wraps the original array.
Backbone provides functions from Underscore.js -> http://documentcloud.github./backbone/#Collection-Underscore-Methods
本文标签: backbonejsDifference () and () in javascriptStack Overflow
版权声明:本文标题:backbone.js - Difference _() and () in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744215288a2595610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论