Symbols for Unique Identifiers
123456789101112const id1 = Symbol("userID"); const id2 = Symbol("userID"); const user = { name: "Alice", [id1]: 101, [id2]: 202 }; console.log(user[id1]); // 101 console.log(user[id2]); // 202 console.log(JSON.stringify(user)); // { name: 'Alice', [Symbol(userID)]: 101, [Symbol(userID)]: 202 }
Symbols are a primitive data type in JavaScript used to create unique identifiers. When you create a symbol using Symbol("description"), each symbol is guaranteed to be unique—even if the descriptions are the same. This makes symbols especially useful for adding properties to objects when you want to avoid accidental name collisions with other code or libraries.
You should use symbols when you need to add a property to an object that should not interfere with existing or future property names. Unlike strings, symbols are not enumerable in standard loops like for...in, so they are often used to define special object properties that are meant for internal use or to add metadata.
Symbols are commonly used in situations such as building libraries, frameworks, or when you want to ensure that certain object keys remain private or do not clash with others. By using symbols, you protect your data from being accidentally overwritten or accessed by unrelated code.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain why the output of JSON.stringify(user) does not include the symbol properties?
How can I access all symbol properties of an object?
Are there any downsides to using symbols as object keys?
Awesome!
Completion rate improved to 6.25
Symbols for Unique Identifiers
Stryg for at vise menuen
123456789101112const id1 = Symbol("userID"); const id2 = Symbol("userID"); const user = { name: "Alice", [id1]: 101, [id2]: 202 }; console.log(user[id1]); // 101 console.log(user[id2]); // 202 console.log(JSON.stringify(user)); // { name: 'Alice', [Symbol(userID)]: 101, [Symbol(userID)]: 202 }
Symbols are a primitive data type in JavaScript used to create unique identifiers. When you create a symbol using Symbol("description"), each symbol is guaranteed to be unique—even if the descriptions are the same. This makes symbols especially useful for adding properties to objects when you want to avoid accidental name collisions with other code or libraries.
You should use symbols when you need to add a property to an object that should not interfere with existing or future property names. Unlike strings, symbols are not enumerable in standard loops like for...in, so they are often used to define special object properties that are meant for internal use or to add metadata.
Symbols are commonly used in situations such as building libraries, frameworks, or when you want to ensure that certain object keys remain private or do not clash with others. By using symbols, you protect your data from being accidentally overwritten or accessed by unrelated code.
Tak for dine kommentarer!