MDN defines the usage of the spread operator as acting for arrays only.
For example, a proper usage for the spread operator looks like
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes']; // ["head", "shoulders", "knees", "and", "toes"]
where we have applied the spread operator on the array parts
.
But in the React docs, they use the spread operator on objects like so
var props = {};
props.foo = x;
props.bar = y;
var component = <Component {...props} />;
If I actually try to do this in the console, I receive a TypeError
var a = {b:1,c:2}
console.log(...a); // TypeError
My questions are:
- Is using the spread operator over objects a standard implemented in the spec (and this use case was somehow omitted from the MDN docs and the console example I tried to show was not done correctly thus giving the TypeError)?
- If not a standard implementation, what is going on under the hood to allow the React component to finesse the spread operator to act in the non-standard method that it does (i.e. acting on objects), and suppress the TypeError that would inevitably arise when doing so?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…