Standard conventions for indicating a function arg

2019-01-15 12:15发布

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?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-15 12:59

Just so we have an example to work from, this is fairly common with jQuery's $.each where you're writing code that doesn't need the index, just the value, in the iteration callback ($.each is backward relative to Array#forEach):

$.each(objectOrArrayLikeThing, function(_, value) { }
    // Use value here
});

Using _ is the closest I've seen to a standard way to do that, yes, but I've also seen lots of others — giving it a name reflective of its purpose anyway (index), calling it unused, etc.

查看更多
够拽才男人
3楼-- · 2019-01-15 13:02

With browsers supporting destructuring one can do:

function ({}, {}, value) {
  // ...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.).

查看更多
登录 后发表回答