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

本文标签: javascriptWhat is the reason for prefixing unused parameters with an underscore ()Stack Overflow