Mouhammad Mahmoodi
Mouhammad Mahmoodi

Reputation: 27

How do Object.getOwnPropertySymbols and Reflect.ownKeys handle Symbols in Javascript?

No two Symbols are equal in JavaScript. So Symbol('foo') == Symbol('foo') is false. If I have an object with a Symbol property I cannot access that property if I hadn't already saved that symbol in a variable.

obj = {[Symbol('foo')]: 'bar'};
console.log(obj[Symbol('foo')]); // undefined

But using Object.getOwnPropertySymbols and Reflect.ownKeys I can retrieve Symbol property:

console.log(obj[Object.getOwnPropertySymbols(obj)[0]]); // bar
console.log(obj[Reflect.ownKeys(obj)[0]]); // bar

How these two static methods can access the same Symbol. Do they have access to internal structure of Symbol repository?

Upvotes: 1

Views: 56

Answers (0)

Related Questions