admin管理员组文章数量:1245032
I have a Node.js app where I have a GET
method on root ('/') that has a request and a response parameter on the callback function. I am making use of the response parameter, however, I am not using the request parameter.
I am using Visual Studio Code, and the linter has suggested to add an underscore as a prefix to my request parameter.
Why should I add an underscore prefix to an unused parameter?
I have a Node.js app where I have a GET
method on root ('/') that has a request and a response parameter on the callback function. I am making use of the response parameter, however, I am not using the request parameter.
I am using Visual Studio Code, and the linter has suggested to add an underscore as a prefix to my request parameter.
Why should I add an underscore prefix to an unused parameter?
Share Improve this question asked Nov 30, 2019 at 19:58 Toby CookToby Cook 3596 silver badges23 bronze badges 1-
1
Very likely just a convention thing. Single unused parameters are often named as just
_
, and personally, private members start with underscore. It doesn't look like it has any semantic relevance. – ASDFGerte Commented Nov 30, 2019 at 20:03
2 Answers
Reset to default 13The main purpose is to inform yourself or other programmers in the future that even though this callback method takes two parameters, I don't need the first one, in your case the req parameter. It slightly improves readability and the intention of your code.
I have this situation in a ES6 module I am exporting. VS Code linter also suggested the underscore on the unused parameter. If I don't include the parameter, I get an error in the imported file. Adding the underscore, the parameter is no longer grayed out. As if it was used in the function it is applied to. One more little pearl of wisdom in JavaScript to hang on to ......
本文标签: javascriptWhat is the reason for prefixing unused parameters with an underscore ()Stack Overflow
版权声明:本文标题:javascript - What is the reason for prefixing unused parameters with an underscore (_)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740210025a2241824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论