Standard conventions for indicating a function argument is unused in JavaScript

前端 未结 5 968
失恋的感觉
失恋的感觉 2020-11-28 14:19

Are there any standard ways of marking a function argument as unused in JavaScript, analogous to starting a method argument with an underscore in Ruby?

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 14:31

    With browsers supporting destructuring one can do:

    function ({}, {}, value) {
      // console.log(value)
    }
    

    Which is kind of neat in that it avoids the problem of multiple arguments having the same name and also won't create problems with libraries that assign methods to _ (lodash, underscore, etc.).

    One problem with this approach is that unused arguments of type undefined or null will throw.

    For undefined one solution is to use default parameters:

    function ({}={}, {}={}, value) {
      // console.log(value)
    }
    

    Sadly no such easily applied solution for null.

提交回复
热议问题