Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

javascript - I don't understand about spread syntax inside objects

I don't understand about spread syntax inside objects.

console.log(...false) // TypeError not iterable
console.log(...1) // TypeError not iterable
console.log(...null) // TypeError not iterable
console.log(...undefined) // TypeError not iterable

I understand above codes that occurs error because of none-iterator.

But these codes are working well.

console.log({...false}) // {}
console.log({...1}) // {}
console.log({...null}) // {}
console.log({...undefined}) // {}

Please let me know why the above codes are working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no spread operator!

This is quite important in order to understand what's happening, so I have to start with it.

There is no spread operator defined in the language. There is spread syntax but as a sub-category of other types of syntax. This sounds like just semantics but it has a very real impact on how and why ... works.

Operators behave the same way every time. If you use the delete operator as delete obj.x, then you always get the same result regardless of context. Same with typeof or perhaps even - (minus). Operators define an action that will be done in the code. It's always the same action. Someimes operators might be overloaded like +:

console.log("a" + "b"); //string concatenation
console.log(1 + 2);     //number addition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...