Symbol
.Symbol
function.Symbol
function must be invoked without the new
keyword.
const sym = Symbol();
const obj = {};
obj[sym] = "hello";
console.log(obj[sym]); // hello
const sym = Symbol();
const obj = {
[sym]: "hello"
};
console.log(obj[sym]); // hello
const name = Symbol();
const person = {
[name]: "John Doe"
};
console.log(person.name); // undefined
console.log(person["name"]); // undefined
console.log(person[name]); // John Doe
const name = Symbol();
const person = {
[name]: "John Doe",
age: 20
};
console.log(Object.getOwnPropertyDescriptors(person));
/*
{
age: { value: 20, writable: true, enumerable: true, configurable: true },
[Symbol()]: {
value: 'John Doe',
writable: true,
enumerable: true,
configurable: true
}
}
*/
console.log(Object.getOwnPropertySymbols(person));
// [ Symbol() ]
Symbol.toPrimitive
Symbol.toStringTag
Symbol.isConcatSpreadable