preface
Hello, guys. I wonder if you have encountered such a situation: in our daily development, we may sometimes use some objects provided by others, and the business needs to expand and add some properties or methods based on this object. At this time, if we rashly extend without understanding the object structure, it is likely to conflict with the original properties or methods of the object. Because the attribute names of objects are named in the form of strings in ES5, it is easy to cause attribute name conflicts. The little knowledge we want to share next can solve this problem well. That is a new original data type - Symbol in ES6
Original data type Symbol
Symbol is a new original data type in ES6. Use it to represent a unique value. Like NaN we learned before, NaN and NaN are always unequal. Similarly, symbol and symbol are always unequal. Next, let's look at the usage and characteristics of symbol
- Symbol usage:
- The usage of Symbol is very simple. The Symbol value is generated through the Symbol function. Function can pass a string type parameter as a description of the Symbol value
let s1 = Symbol(); let s2 = Symbol('hello');
- The usage of Symbol is very simple. The Symbol value is generated through the Symbol function. Function can pass a string type parameter as a description of the Symbol value
- Characteristics of Symbol
- The value of Symbol is unique, and each Symbol value is different
- The Symbol value is generated through the Symbol function. The Symbol function can accept a string as a parameter to represent the description of the Symbol instance
- If the Symbol function passes an object as a parameter, it will call the toString method of the object to convert the object into a string
- Symbol value cannot be calculated with other types of values, otherwise an error will be reported
- Symbol values can be explicitly converted to strings or Booleans, but not numbers
- Symbol as the attribute name of an object needs to be wrapped in square brackets []
- When Symbol value is used as object property name, dot operator cannot be used. It needs to be accessed in the form of object name [Symbol variable]
- Symbol type can also define a set of constants to ensure that the values of these constants are not equal
- When Symbol is used as an object property name, it will not appear in for... In, for... Of, and will not be returned by Object.keys(), Object.getOwnPropertyNames(), JSON.stringify(). However, the property name of Symbol type can be obtained through the Object.getOwnPropertySymbols() method
//1. The value of symbol is unique //2. The Symbol value is generated through the Symbol function let s1 = Symbol(); let s2 = Symbol(); console.log(s1===s2); //false //3. The Symbol function can accept a string as a parameter to represent the description of the Symbol instance let s3 = Symbol('description'); let s4 = Symbol('description'); s3 === s4 ;//false //4. Pass an object as a parameter to the Symbol function let s5 = Symbol([1,2,3]) console.log(s5); // Symbol(1,2,3) //5. Symbol value cannot be calculated with other types of values let s6 = Symbol('hello') console.log(s6 + " world");// TypeError: can't convert symbol to string //6. Symbol value can be explicitly converted to string or Boolean value, but cannot be converted to number let s7 = Symbol('1') String(s7);// Symbol(1) s7.toString();// Symbol(1) typeof s7 ;// "string" Boolean(s7) //true Number(s7);// TypeError: can't convert symbol to number //7. When symbol is used as the attribute name of the object, it needs to be wrapped in square brackets [] //8. When Symbol value is used as object attribute name, dot operator cannot be used. It needs to be accessed in the form of object name [Symbol variable] let s8 = Symbol('obj'); let obj = { [s8]: 'hello world' } console.log(obj.s8);//undefined this case s8 will be considered as a string attribute, so it cannot be obtained console.log(obj[s8]);//hello world //9. Symbol type can also define a set of constants to ensure that the values of these constants are not equal const log = {}; log.levels = { DEBUG: Symbol('debug'), INFO: Symbol('info'), WARN: Symbol('warn') }; console.log(log.levels.DEBUG, 'debug message'); console.log(log.levels.INFO, 'info message'); //10. When a Symbol is used as an object property name, it will not appear in for...in and for...of, nor will it be returned by Object.keys(), Object.getOwnPropertyNames(), JSON.stringify(). However, the property name of the Symbol type can be obtained through the Object.getOwnPropertySymbols() method const obj = {}; let a = Symbol('a'); let b = Symbol('b'); obj[a] = 'Hello'; obj[b] = 'World'; obj.c = 'javascript' Object.keys();// ['c'] Object.getOwnPropertySymbols(obj);//[Symbol(a), Symbol(b)]
Usage scenario of Symbol
After understanding some characteristics of Symbol, you can do something with the help of these characteristics. such as
- Extend objects provided by others
- Eliminate magic string
- Define some non private methods for objects that you want to use only internally
- Define a set of constants, and so on
summary
Today, we learned a new knowledge Symbol in ES6. I believe our friends also feel the power of Symbol. The use and features of Symbol are shared here. The next article will continue to explore some of the built-in values in symbols and their roles. Thank you for your support.
Like the little partner, welcome to like the message and pay attention!