Symbol usage of js

Symbol

1. Symbol usage

let s = Symbol();
console.log(s);

Symbol s will never be the same

Symbol is a string that will never appear the same. When we declare the same variable, if it occurs repeatedly, only the variable declared for the first time will be printed.

let a = Symbol();
let b = Symbol();
console.log(a == b); // false

You can't press attributes inside

let a = Symbol();
a.name = "Symbol";
console.log(a.name); 

Add a description

toString() prints its string

Description print out the description of the Symbol

let a = Symbol('Symbol study');
console.log(a.description); 

2. Another way to define Symbol

Using this method, symbol will open up a memory record in memory. If duplicate variables are used, symbol can be used For(). Symbol.keyFor() can get the description of the symbol.

let symbol = Symbol.for('Symbol.for study');
let symbol1 = Symbol.for('Symbol.for study');
console.log(symbol == symbol1); // true
console.log(Symbol.keyFor(symbol)); // Get a description of the Symbol

Use Symbol For definitions will be saved globally, while those defined with ordinary symbols are not defined globally, so we can't read them.

let symbol = Symbol('Symbol study');
let symbol1 = Symbol.for('Symbol.for study');
console.log(Symbol.keyFor(symbol)); // Cannot read
console.log(Symbol.keyFor(symbol1)); // Can read

3. Use of symbols

User1 and user2 are obtained through the declaration of objects, and their two keys are bound through symbols. In grade, user1 Key needs to be wrapped with '[]', otherwise it will be treated as a string, [user1,key] means to get the corresponding variable. Finally, you can complete the call through the element you want to find.

let user1 = {
            name: 'Li Si',
            key: Symbol()
        };
        let user2 = {
            name: 'Li Si',
            key: Symbol()
        };
        let grade = {
            [user1.key]: {
                js: 100,
                css: 89
            },
            [user2.key]: {
                js: 66,
                css: 55
            }
        }
        console.log(grade[user1.key]);

4. Use of Symbol in cache container

In order to make better use of this Symbol, we often share common things in a container.

  • Declare the variable below, and then specify a specific value in an element for output.
 let user = {
            name: "apple",
            dese: "User information",
        };
        let cart = {
            name: "apple",
            dese: "Shopping Cart",
        };
        Cache.set('user-apple', user);
        Cache.set('cart-apple', cart);
        console.log(Cache.get("cart-apple"));
  • Bind the Symbol through the key to complete the non repeated call
 let user = {
            name: "apple",
            dese: "User information",
            key: Symbol('Member information')
        };
        let cart = {
            name: "apple",
            dese: "Shopping Cart",
            key: Symbol('Shopping cart information')
        };
        Cache.set(user.key, cart);
        Cache.set(cart.key, cart);
        console.log(Cache.get(user.key));

expand

When we cannot publish data or change elements as private attributes, we can use symbols, but after using symbols, we can't use for in and for of to call the elements in the attribute. If we want to get the elements in the symbol, we can only use getOwnPropertySymbols; If you want to get the common variables of the array and the attributes of symbol, you can use reflect Ownkeys gets.

 <script>
        let symbol = Symbol('This is a Symbol type');
        let a = {
            name: 'abc',
            // Consider it private and protected
            [symbol]: 'abcdefg'
        };
        // Symbol cannot be traversed with for in
        // for (const key in a) {
        //     console.log(key); // name
        // }

        // Symbol cannot be traversed by using for of
        for (const key of Object.keys(a)) {
            console.log(key);
        }

        // If you want to traverse the properties of symbols, you can use getOwnPropertySymbols, which cannot traverse ordinary types
        for (const key of Object.getOwnPropertySymbols(a)) {
            console.log(key);
        }

        // Reflect.ownKeys traverses all properties
        for (const key of Reflect.ownKeys(a)) {
            console.log(key);
        }

        let site = Symbol("This is Symbol");
        class User {
            constructor(name) {
                this.name = name;
                this[site] = "symbol"
            }
            getName() {
                return `${this[site]} ${[this.name]}`;
            }
        }
        let user1 = new User('Li Si');
        console.log(user1.getName()); // symbol Li Si

        for (const key in user1) {
            console.log(key); // Only name can be seen
        }
    </script>

Keywords: Javascript

Added by killsite on Thu, 10 Feb 2022 18:37:36 +0200