Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Symbol type of javascript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use the Symbol type of javascript", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Symbol type of javascript" article.

Symbol is a new primitive data type that represents a unique. Is the seventh data type in javascript. The other six are: undefined, null, String, Number, Object

Declaration mode

The Symbol value is generated by the Symbol function. There can be two types of property names for an object, one is the existing string, and the other is the new Symbol type. Property names that belong to the Symbol type are unique and are guaranteed not to conflict with other property names.

The let s1=Symbol () let s2=Symbol () console.log (S1) / / Symbol () console.log (S2) / / Symbol () console.log (s1===s2) / / false//Symbol function takes a string as an argument to indicate the description of the Symbol instance let s1=Symbol ('xxx') let s2=Symbol (' hhh') console.log (S1) / / Symbol (xxx) console.log (S2) / / Symbol (hhh) console.log (s1===s2) / / false copy code

The new command cannot be used before the Symbol function, an error will be reported. This is because the generated Symbol is a primitive type value, not an object. That is, because the Symbol value is not an object, attributes cannot be added. It's like a special kind of string.

Symbol.for () globally defines Symbol

Symbol.for () takes a string as a parameter and searches for a Symbol value with that parameter as its name. If so, the Symbol value is returned, otherwise a new Symbol value with the name of the string is created and registered globally.

Let S1 = Symbol.for ('xxx') let S2 = Symbol.for (' xxx') console.log (S1 = = S2) / / truefunction foo () {return Symbol.for ('hello')} const x=foo () const y=Symbol.for (' hello') console.log (x = y) / / true

Both Symbol.for () and Symbol () generate a new Symbol. The difference is that the former will be registered in the global environment for search, while the latter will not. Symbol.for () does not return a new value of type Symbol every time it is called. Instead, it checks whether the given key already exists before creating a new value.

Symbol.keyFor ()

The Symbol.keyFor () method returns the key of a registered Symbol type value.

Const S1 = Symbol ('foo') console.log (Symbol.keyFor (S1)) / / undefinedconst S2 = Symbol.for (' foo') console.log (Symbol.keyFor (S2)) / / foo application scene as attribute name

Because the Symbol values are not equal, this means that the Symbol value can be used as an identifier and used in the property name of the object to ensure that an attribute with the same name will not appear. This is useful in situations where an object is made up of multiple modules to prevent a key from being accidentally overwritten or overwritten.

Const grade= {address:'qqq',tel:'111'}, Li Si: {address:'aaa',tel:'222'}, Li Si: {address:'sss',tel:'333'},} console.log (grade) / / Zhang San: {address: "qqq", tel: "111"} Li Si: {address:" sss " Tel: "333"} / / the key value of the object cannot be duplicated. If the value value is repeated, it will overwrite the previous / / solve with Symbol. Equivalent to a unique string const stu1=Symbol ('Li Si') const stu2=Symbol ('Li Si') console.log (stu1===stu2) / / falseconst grade= {[stu1]: {address:'aaa',tel:'222'}, [stu2]: {address:'sss',tel:'333'},} console.log (grade) / / Li Si: {address:'sss',tel:'222'} Li Si: {address:'sss' Tel:'333'} console.log (grade [stu1]) / / Li Si: {address:'sss',tel:'222'} console.log (grade [stu2]) / / Li Si: {address:'sss' The method of tel:'333'} attribute traversing const sym=Symbol ('imooc') class User {constructor (name) {this.name=name This [sym] =' imooc.com'} getName () {return this.name+this [sym]}} const user=new User ('www') / / for in cannot traverse the Symbol attribute like for (let key in user) {console.log (key) / / name} / / the Object.keys (obj) method cannot traverse the Symbol attribute for (let key of Object.keys (user)) {console.log (key) / / name} / / Object.getOwnPropertySymbols (obj), only the Symbol attribute for (let key of Object.getOwnPropertySymbols (user)) {console.log (key) / / Symbol (imooc)} / / Reflect.ownKeys (obj) object can be obtained for (let key of Reflect.ownKeys (user)) { Console.log (key) / / name / / Symbol (imooc)} eliminate magic strings

A magic string refers to a specific string or value that appears multiple times in the code and forms a strong coupling with the code. For code with good style, you should try to eliminate the magic string and replace it with variables with clear meaning.

Function getArea (shape) {let area = 0 switch (shape) {case 'Triangle': area = 1 break case' Circle': area = 2 break} return area} console.log (getArea ('Triangle')) / / Triangle and Circle are magic strings. It occurs many times and forms a "strong coupling" with the code, which is not conducive to subsequent modification and maintenance. Const shapeType = {triangle: Symbol (), circle: Symbol ()} function getArea (shape) {let area = 0 switch (shape) {case shapeType.triangle: area = 1 break case shapeType.circle: area = 2 break} return area} console.log (getArea (shapeType.triangle)) these are the contents of this article on "how to use Symbol types of javascript" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report