Symbol Data TypeIn the js language, there are 6 data types before ES6. ES6 newly proposes the symbol data type, so symbol is the seventh data type of js, representing a unique value. Is a data type similar to a string. The purpose is to prevent attribute name conflicts and ensure that each attribute name in the object is unique. let s1 = Symbol('foo'); let s2 = Symbol('foo'); s1 === s2 // false The Symbol type can have a string parameter that represents the description of the Symbol instance. Therefore, two Symbol type instances with the same description are not equal. The reason why symbol appearsThe object property names of ES5 are all strings, which can easily cause property name conflicts. For example, if you use an object provided by someone else, but want to add a new method to this object (mixin mode), the name of the new method may conflict with the existing method. It would be nice if there was a mechanism to ensure that each attribute name is unique, thus preventing attribute name conflicts from occurring. This is why ES6 introduced Symbol Symbol Features
//Create Symbol let s = Symbol(); console.log(s, typeof s); // Try to create 2 symbols with the same name let s2 = Symbol(' 辣鸡rb'); let s3 = Symbol(' 辣鸡rb'); console.log(s2 === s3); //false //Use Symbol.for to create the same symbol let s4 = Symbol.for('spicy chicken rb'); let s5 = Symbol.for('spicy chicken rb'); console.log(s4 === s5); //true //Cannot perform operations with other data let result = s + 100; //Error, At the end of the article, let's review the data types of js A mnemonic from Silicon Valley // USONB =>you are so .niubility You are so awesome // u=>undefined // s=>string symbol // 0=>object // n=>null number // b=>boolean After thinking about it, I decided to write more. Application of symbolsAdd up and down methods to the rb objectMethod 1 let rb = { name: 'Japanese war criminals', age: 500, }; // Processing with symbol // Declare an object, which contains two methods. The methods are written with symbol() let methods = { up: Symbol(), down: Symbol() }; // Add the method rb[methods.up] = function () { console.log('Forgive me for talking about people'); }; rb[methods.down] = function () { console.log('This beast has no shame and asks the Chinese people to forgive it'); }; console.log(rb); Method 2 Add sb and dsb methods to rb object let rb = { name: 'Japanese war criminals', age: 500, [Symbol('sb')]: function () { console.log('I like Japanese animation'); }, [Symbol('dsb')]: function () { console.log('But it doesn't stop me from hating the crimes they committed in China'); }, }; console.log(rb); Symbol built-in property values
SummarizeThis is the end of this article about the symbol data type in ES6. For more information about the symbol data type in ES6, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Docker exposes port 2375, causing server attacks and solutions
1. Rendering 2. Source code HTML < body > &...
Find the problem I recently encountered a problem...
Preface A requirement I had previously made, to s...
1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...
The main configuration file of Nginx is nginx.con...
Table of contents Preface 1. The process of using...
To beautify the table, you can set different bord...
Table of contents Summary of Distributed ID Solut...
x-ua-compatible is used to specify the model for ...
The implementation of expanding and collapsing li...
1. Overlay Overview Overlay means covering, as th...
Table of contents 1 element offset series 1.1 Off...
Table of contents Boolean Type Number Types Strin...
Solve the problem that the vue project can be pac...
Table of contents Basic syntax for multi-table jo...