Why are there advantages to using both spread operator and destructuring assignment in function parameters?
P粉775723722
P粉775723722 2023-08-16 16:27:57
0
1
467
<p>I keep encountering this syntax, but I'm having trouble understanding what exactly it's doing: </p> <pre class="brush:php;toolbar:false;">export class SomeClass extends SomeParent { constructor(...[configuration]) { // Only reference the "configuration" line of code } }</pre> <p>After trying it in Node REPL, I found that there is no difference between the following two ways of writing: </p> <pre class="brush:php;toolbar:false;">function foo(...[bar]) { console.log(bar); console.log(arguments) }</pre> <p>...and...</p> <pre class="brush:php;toolbar:false;">function foo(bar) { console.log(bar); console.log(arguments) }</pre> <p>...So what does it do? </p>
P粉775723722
P粉775723722

reply all(1)
P粉670107661

It does seem pointless. You need to ask the author of the code what their intentions are in this regard, and they should at least leave a comment.

However, there is actually a slight difference: the remaining parameters do not count towards the function's number of parameters. Therefore, (function(bar){}).length is 1 and (function(...[bar]){}).length is 0.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template