Javascript: what does function(_) mean

雨燕双飞 提交于 2019-11-29 06:46:36

In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter".

Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a "throw-away" identifier is found more commonly in other languages, but consider a case like arr.forEach(function (_, i) {..}) where _ indicates the first parameter is not to be used.

It's an anonymous function with one argument, the name of that argument is _.

I don't know why they bother with the argument, since the function doesn't use it.

It's the same as putting any other identifier to a list of arguments according to this document: http://mathiasbynens.be/notes/javascript-identifiers

You'll find in this doc that _ is a legal character that an identifier can start with.

There is no any meaning for this in your example, probably the author just thought that it's cooler than just ().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!