最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the reason for prefixing unused parameters with an underscore (_)? - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 13

The 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 ......

发布评论

评论列表(0)

  1. 暂无评论