admin管理员组文章数量:1403107
I came across the following code fragment in a CouchDB book.
function(doc) {
doc.tags && doc.tags.forEach(function(tag) {
emit(tag, 1);
});
}
Can some one explain how does the function(tag) part works?
Thanks and regards,
raj
I came across the following code fragment in a CouchDB book.
function(doc) {
doc.tags && doc.tags.forEach(function(tag) {
emit(tag, 1);
});
}
Can some one explain how does the function(tag) part works?
Thanks and regards,
raj
Share Improve this question edited Oct 23, 2009 at 2:22 Sasha Chedygov 131k26 gold badges106 silver badges116 bronze badges asked Oct 22, 2009 at 11:08 Rajkumar SRajkumar S 2,5216 gold badges23 silver badges29 bronze badges3 Answers
Reset to default 9This is called an anonymous inline function expression. It creates a function and gives you a reference to it, similar to if you had written:
function emitTag(tag) {
emit(tag, 1);
}
doc.tags && doc.tags.forEach(emitTag);
The array.forEach
method calls the given function once for each of the items in array
in order. It is a standard method in ECMAScript Fifth Edition and has been in many browsers for some time, but not JScript (IE). I am guessing couchdb takes care of that issue for you though.
function(tag) {...}
gets called "for each" tag in "doc.tags" with the "tag" argument passed to the lambda function in question.
forEach
simply iterates over array and calls function you pass to it with every element it finds.
Be aware that not every browser support it, there's helper function $.forEach
in jQuery, it is safer in terms of browser support.
本文标签: couchdbMeaning of forEach(function(tag) in JavaScriptStack Overflow
版权声明:本文标题:couchdb - Meaning of forEach(function(tag) in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744341020a2601474.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论