符号

从 ECMAScript 2015 开始,symbol 是原始数据类型,就像 numberstring 一样。

symbol 值是通过调用 Symbol 构造函数创建的。

let sym1 = Symbol();

let sym2 = Symbol("key"); // optional string key

符号是不可变的,并且是唯一的。

let sym2 = Symbol("key");
let sym3 = Symbol("key");

sym2 === sym3; // false, symbols are unique

就像字符串一样,符号可以用作对象属性的键。

const sym = Symbol();

let obj = {
  [sym]: "value",
};

console.log(obj[sym]); // "value"

符号也可以与计算属性声明相结合来声明对象属性和类成员。

const getClassNameSymbol = Symbol();

class C {
  [getClassNameSymbol]() {
    return "C";
  }
}

let c = new C();
let className = c[getClassNameSymbol](); // "C"

unique symbol

2uwo0532pWSASvG0Yi6WZntcl85S44ExwFq15P1JgoRGs3ZNavHcm4JQiAWA/oEgaM4dFBFZR6MR48yKHris0DKa3ZfGNUQ8NcH7FTNMLAdSBIGhp9hGuRSNQLVh4RcnNnR7oYHQdbcmNly/oOO5xiJziLcuQaHrd0jAwdYDRz+EvDK0RGdKHp6YfOYbaRys+3nLUeLbBA/TrlJpm6FCG5MHLVWqrWd1cnUXfsQlF02YgmoyIBPNq/TKicvx1OvxbGswJxeV5pZR1YRNypFtssqCTIzoV1Pd0rJkDlEidOaMJ8kMQRuc2cD0h3JlqXz83yPAYVsFN7QLEVMhcLSz4hHnwz63lGnVyRKFzRgTLR111J8EiXquhQNlUl0eIV9wR/6XFT9JejxurhMCYSAOpJ/LJpHyR6+gVZAXSME3TXqun6PfVdF5CX/1y6J4ZTjFh//Gsn3g90hTZBiJ+GJ8UoPN9rqCWvoYv3FEwkzh/eAIrB0c5wiEQi4oP8T+0xmzPy9op+xtKCkj+jhESK/ifhZ7frOzK6KziW0C/N9tSGQ99DemepX4QN+u4Mda1th5kK16Zvf2sNWNjFL+Z/9Jv2vG4rO7SpZrJQ1IhCuPPaJfiUl1+SxV28wOX+O9kZog3b001QzgPTOk5TDA5PJ1stO9mcgH8xtT4IBvhn7EwA9hx0b5ni1Iv58KmOpp8zuIdoAwjauk4ka7iwbtRGaXWw==

declare const sym1: unique symbol;

// sym2 can only be a constant reference.
let sym2: unique symbol = Symbol();

// Works - refers to a unique symbol, but its identity is tied to 'sym1'.
let sym3: typeof sym1 = sym1;

// Also works.
class C {
  static readonly StaticSymbol: unique symbol = Symbol();
}

LaW9ojteu4Ey2K9LH6GKzQwMBlqG7xwkFZUfVrZCqOaTc0rU9BWregnp7kMN9CcmYgmajXcffSONtD+9o3xVg4a+BL905JgaewRHm1d+itcPpls5t/owK/MdfYj7ELDmaDQRcrzP344Z40TQDK+RuGaJX80v9xuY3HygWRZ4APdx9Vvur25uNaoV0BdNiefaODNmcsSInOC/qDsHVyUVbg==

const sym2 = Symbol();
const sym3 = Symbol();

if (sym2 === sym3) {
  // ...
}

知名符号

Fk/jyH7W4yY3D+CwX1eH61+6G7YNy5PZLwcfKWYjVtHfJkhn687rJCteGhKRqURxFfYUZrHwsqrtkZWjyeg4tlguieGo7qX3Bnpsfa0sRvCg5fIOF5eQfsRAQXpFH7to7cSvIw17TLMc9A1IFAobLKy1LOntPpF3K3tj1xEHU0M=

27zVVg9Fpg23SXPyFUxSFydZiK04i3CmFXNae1Aflujkvxf6t1fuU0otUm96IhPZ

Symbol.hasInstance

QnxyHupHIDy37VRAzIi+j4VvV2wBafWYNYncHFtgJeYT35BPUYO1cs0IB8f8eti6SbUv1Sf+XX23/5CNpu3F8Woq/u8XyBUzbsG4piLtIL/4XyRArwSbvt1meHHPNFihn4uOOBBfbsc4+9x4qM1E1RNiTTFE0u6YmpuPSNWsFYpdda7vaBTJdsn9mWOw2Ul6

Symbol.isConcatSpreadable

/u9kGDxo8CaqSjcWvkrPPKJPmf6kwFwt+Hd+IAKzs49TQ0q0vNjbGGTnESrg20OJPtwgBnDRXpMwk5DqrkhLki6clUYYpglAm+J03Xt6Vmqvya10JNvRv2ZtLQVfg6zs

Symbol.iterator

55UDlcF9eQFF3BkrkBGfCAh00ZrD0lp2e1PKIyv0IO20pQGDcbh+GqOhK/O8d3LiOPhWO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C8y7E9ORVfg2eI5dVkxenHTGN+eWp7bg6BG+Q==

Symbol.toStringTag

+t4DUpqPDvtJKFkynb1V3ZFf/YZTmVT9zX1DYJo+GwlYgcecCAEHp51toJD44cpjDugEk7UfrQ667JdMwmAADAbOxZxGRR3luZ8/37fAsr6QXKt1+SgWf/qZt2/SCGNKfx0LI5dMOAj2e+lZALkR2Wom67uyHVkIZ7tjw89l/AKX9wh82rKqtoJYuYWusIdg

Symbol.unscopables

Lp0nAAKOBEa0x9YBpuPfY/NlZtF32FgQpVU3udohRJKlnvB0IjsJ2d28r7Rl0tVoPGsj45qHRUePhwly6sftMZNEUOvCJWDIH1bwbdTJYViqbWveEnaC7jBdBngmq9VMnsB3ZDZhnRWVkWzCERg5zw==