In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use symbol in es6, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use symbol in es6. Let's take a look at it.
In es6, Symbol can be used as an attribute name, definition variable, or constant to represent a unique value. It is a new data type in es6. Two variables declared by Symbol can never be equal, and the syntax is "const name=Symbol (" value ")".
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What is the use of symbol in es6
What is Symbol?
Symbol is a newly added data type in es6, which represents a unique value. In es5, we divide the data types into basic data types (string, number, Boolean, undefined, null) and reference data types (Object). The new Symbol data types in es6 are divided into basic data types.
Why is there such a data type?
/ / someone gives you a defined object var obj = {name: "xiaoqiang", showName: function () {alert (1)}} / / when you get the object and want to add new properties and methods to the object, you may create a name property and showName method obj.name = "nodeing" obj.showName = function () {alert (2)} / / at this time, the new methods and properties will overwrite the original method This leads to conflict.
Since the properties and methods of an object are composed of strings, it is possible to conflict when adding methods and properties, so a new data type has been added in es6 to represent a unique value.
Through the Symbol function, you can create a unique value, and each time the Symbol function is executed, it returns an object.
Let S1 = Symbol () let S2 = Symbol () console.log (S1 = = S2) / / false indicates that S1 and S2 created are not the same console.log (S1, S2); / / Symbol () Symbol ()
In the above code, the printed values are all Symbol (). How can you tell who is S1 and who is S2? We can solve the problem by passing parameters.
/ / the parameter passed in is the description of the current Symbol, which is used to distinguish between Symbollet S1 = Symbol ("S1") let S2 = Symbol ("S2") console.log (S1, S2); / / Symbol (S1) Symbol (S2) application scenario
Symbol is usually used to set the property name or method of an object to prevent the newly added property or method from conflicts with the original property or method
Let name = Symbol ("name"); let show = Symbol ("show"); let obj = {/ / set property [name]: 'xiaoqiang', [show] () {alert (1)}}; / / value console.log (obj [name]); / / call method obj [show] ()
It is important to note that the value of name must be a variable that can be changed in [].
Matters needing attention
Note 1: the parameters in Symbol are just descriptions of Symbol and have no other meaning, so even if the description is the same, Symbol is different.
Here you can guess that the return value of the function Symbol is an object.
Console.log (Symbol ("nodeing") = = Symbol ("nodeing")) / / false
Note that the 2:Symbol function cannot be called using the New keyword
Let S1 = new Symbol (); / / error report
Note 3: Symbol types cannot be converted to numbers when doing type conversion
Let S1 = Symbol ("S1"); console.log (String (S1)); / / Symbol (S1) console.log (Boolean (S1)); / / trueconsole.log (Number (S1)) / / error report
Note 4: Symbol cannot do any operation (probably because the returned value cannot be calculated)
Console.log (Symbol ("S1") + "nodeing") / / error console.log (Symbol ("S1")-100) / / error report
Note 5: when Symbol is used as an object property or method, there is no way to take a value if it is not assigned to a variable
Let obj = {/ / set attribute [Symbol ("name")]: 'xiaoqiang'}; / / value console.log (obj [Symbol ("name")])
Note 6: there is no way for Symbol to be traversed by for in loops
Let name = Symbol ('name') let age = Symbol (' age') let obj = {a: 1, b: 2, [name]: 'xiaoqiang', [age]: 18}; for (let attr in obj) {console.log (attr,obj [attr]) / / a b}
You can use Object.getOwnPropertySymbols to view all symbol properties on an object
This is the end of the article console.log (Object.getOwnPropertySymbols (obj)) on "how to use symbol in es6". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use symbol in es6". If you want to learn more, you are welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.