Quiz: Symbols & Enumerables
Do you know the less-famous bits of ES2015?
Quiz: JavaScript Interfaces, Symbols, and Enumerables
- Prove your JavaScript skillz! 🚀
- No login or signup required. ✨
- Multiple choice. 🤖 … How difficult can it be, eh?
Advanced Interfaces
How should you access a JavaScript object property that uses a getter method?
In JavaScript, a getter can be accessed like a normal property. There’s no need to call it like a function.
In this example, accessing obj.val
directly invokes the getter method and outputs got it!
.
Advanced Interfaces
What is the correct way to create a truly unique property key for a JavaScript object?
Symbols are a unique and immutable primitive type that can be used as keys for object properties. This helps in avoiding name collisions, especially in large codebases or when writing reusable libraries.
Advanced Interfaces
Will the property age
be listed during a for...in
iteration?
The enumerable
property in Object.defineProperty()
controls whether the property will show up in enumeration methods like for...in
. In this example, since enumerable: true
, the age
property will be listed during iteration.
Working with Objects
What is the default enumerability of a property when using Object.defineProperty()
without specifying enumerable
?
When you use Object.defineProperty()
without specifying enumerable
, its default value is false
. This means the make
property will not show up in Object.keys()
or other enumeration methods.
Advanced Interfaces
What will be the result of the following comparison?
Each call to Symbol()
creates a unique and immutable value, even if the description is the same. In this case, sym1
and sym2
are different symbols, so the comparison returns false
.
Advanced Interfaces
Will the Symbol-keyed property be listed during a for...in
iteration?
Properties keyed by Symbols are not enumerable in for...in
loops or Object.keys()
. In this example, only regularKey
will be listed, not the Symbol-keyed property.
Advanced Interfaces
Which method can be used to retrieve all Symbol keys of an object?
The Object.getOwnPropertySymbols()
method is used to retrieve all Symbol properties of an object.