admin管理员组文章数量:1244294
With array destructuring, it's possible to discard leading items by inserting mas without a preceding reference:
const [ , two ] = [ 1, 2 ]
The same isn't true of function signatures — the following code won't parse because the leading ma in the signature is unexpected:
function ditchFirstArgument( , second ){}
Why do I need to provide references for leading parameters in ES6 function expressions?
With array destructuring, it's possible to discard leading items by inserting mas without a preceding reference:
const [ , two ] = [ 1, 2 ]
The same isn't true of function signatures — the following code won't parse because the leading ma in the signature is unexpected:
function ditchFirstArgument( , second ){}
Why do I need to provide references for leading parameters in ES6 function expressions?
Share Improve this question asked May 14, 2016 at 16:14 BarneyBarney 16.5k5 gold badges65 silver badges80 bronze badges 9- 4 what kind of answer are you looking for? "Because the language doesn't allow it"? If you are asking for reasons for design decisions, SO is not a good place for this (because most people here don't work on ECMAScript) – Felix Kling Commented May 14, 2016 at 16:17
- 1 this would lead to extremely specialized functions with extremely poor reusability. and even worse: the naming of these functions would be a nightmare :D – user5536315 Commented May 14, 2016 at 16:52
- Hi Felix, I appreciate most people didn't design Javascript but still thought Stack Overflow would be a good place to get a technical explanation of why the language won't let me do this. I fully expect many SO users not to have an answer — that's fine :) – Barney Commented May 14, 2016 at 17:53
- Ivan, Estus, it's mon when writing code for reuse to expose functions which conform to a mon interface: this way programmers don't need to know the internal workings of a function, but can expect it to adhere to a certain signature and return output according to a predefined range of expectations. You may want to write a function this way for the same reasons as the destructuring assignment: in this instance, you don't need some parts of the input. Giving them references in this case is just misleading noise. – Barney Commented May 14, 2016 at 18:00
- 1 Why would you define a function that takes a parameter which is ignored? – user663031 Commented May 14, 2016 at 19:10
3 Answers
Reset to default 5Why do I need to provide references for leading parameters in ES6 function expressions?
Because otherwise it would be a syntax error. In not just ES6 but any version of the language you cannot elide formal parameters because the spec does not provide for it.
If you really want to do this (but why?), you could write it as
function ditchFirstArgument(...[, second]) {}
or at least you will be able to in some future version of ES; see https://github./tc39/ecma262/mit/d322357e6be95bc4bd3e03f5944a736aac55fa50. This already seems to be supported in Chrome. Meanwhile, the best you can do is
function ditchFirstArgument(...args) {
const [, second] = args;
But why does the spec not allow elision of parameters?
You'd have to ask the people who wrote it, but they may have never even considered it, or if they did, rejected it because it's bug-prone, hardly ever necessary, and can easily be worked around using dummy formal parameters like _
.
Roman's insight from Go is useful but inappropriate to JS where the token _
is a valid reference, conventionally used by the Underscore and later Lodash libraries.
Even if that's acceptable, you'd have to create and avoid dud references for every unused argument, which isn't ideal.
However, it is possible to destructure a function argument into an empty object, which effectively nullifies the parameter without reference.
function take_third( {}, {}, third ){
return third
}
EDIT: As Paul points out in the ments, this will throw if any of the skipped parameter values are null
or undefined
. undefined
values can be guarded against with default assignments, but this won't work for null
:
function take_third( {} = {}, {} = {}, third ){
return third
}
I believe it's a mon pattern to name unused variables with an underscore:
function ditchFirstArgument(_, second) { /* ... */ }
While it would not prevent you from actually using this variable (like in Go), it seems to be a pretty straightforward workaround.
本文标签: javascriptWhy can39t I skip parameter assignments in a function signatureStack Overflow
版权声明:本文标题:javascript - Why can't I skip parameter assignments in a function signature? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740186484a2238303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论