ES6 introduces a bevy of convenient "syntactic sugar". Among them are the default parameter capabilities of JavaScript functions, as well as rest parameters. I'm finding that my console (or devTools) complains (i.e., throws an error) whenever attempting to set a default parameter value on a rest parameter. I've found surprisingly few references to this particular issue elsewhere and am wondering if 1.) it is possible to do so and 2.) why not (assuming it's not possible).
As an example, I've contrived a trivial (but, hopefully still purposeful) example. In this first iteration of the function, I've constructed the function such that it will work (which is to say, without giving the rest parameter a default value).
const describePerson = (name, ...traits) => `Hi, ${name}! You are ${traits.join(', ')}`;
describePerson('John Doe', 'the prototypical placeholder person');
// => "Hi, John Doe! You are the prototypical placeholder person"
However, now with the default:
const describePerson = (name, ...traits = ['a nondescript individual']) => `Hi, ${name}! You are ${traits.join(', ')}`;
describePerson('John Doe');
// => Uncaught SyntaxError: Unexpected token =
Any help is greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…