In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the role of symbol in es6 related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have a harvest after reading this article on the role of symbol in es6, let's take a look.
The role of symbol in es6: 1, used as an attribute name, symbol represents a unique value, which ensures that the attribute does not have the same name, and can also be accessed outside the class; 2, used to define constants, is a new original data type, syntax is "const name=Symbol (" value ")".
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What is the function of symbol in es6
ES6 introduces a new primitive data type, Symbol, which represents a unique value, and the greatest use is to define the unique property name of the object.
The ES6 data type adds Symbol in addition to Number, String, Boolean, Object, null, and undefined.
Basic usage
The Symbol function stack cannot use the new command because Symbol is a primitive data type, not an object. You can take a string as a parameter to provide a description for the newly created Symbol, which can be displayed on the console or used as a string for easy distinction.
Let sy = Symbol ("KK"); console.log (sy); / / Symbol (KK) typeof (sy); / / "symbol" / / the values returned by the same parameter Symbol () are not equal let sy1 = Symbol ("kk"); sy = = sy1; / / false
As attribute name
Usage
Since the values of each Symbol are not equal, Symbol, as the property name of the object, ensures that the property does not have the same name.
Symbol cannot be used as an object property name. Operator, use square brackets. Because。 Operator is followed by a string, so you get the string sy property instead of the Symbol value sy property.
Let syObject = {}; syObject [sy] = "kk"; syObject [sy]; / / "kk" syObject.sy; / / undefined
Pay attention
When the Symbol value is used as the property name, the property is public, not private, and can be accessed outside the class. But it will not appear in the loop of for...in or for...of, nor will it be returned by Object.keys () or Object.getOwnPropertyNames (). If you want to read the Symbol property of an object, you can get it through Object.getOwnPropertySymbols () and Reflect.ownKeys ().
Let syObject = {}; syObject [sy] = "kk"; console.log (syObject); for (let i in syObject) {console.log (I);} / / No output Object.keys (syObject); / / [] Object.getOwnPropertySymbols (syObject); / / [Symbol (key1)] Reflect.ownKeys (syObject); / / [Symbol (key1)]
Define constant
In ES5, strings are used to represent constants. For example:
Const COLOR_RED = "red"; const COLOR_YELLOW = "yellow"; const COLOR_BLUE = "blue"
But using strings does not guarantee that constants are unique, which can cause some problems:
However, using Symbol to define constants ensures that the values of this set of constants are not equal. Use Symbol to modify the above example.
Const COLOR_RED = Symbol ("red"); const COLOR_YELLOW = Symbol ("yellow"); const COLOR_BLUE = Symbol ("blue"); function ColorException (message) {this.message = message; this.name = "ColorException";} function getConstantName (color) {switch (color) {case COLOR_RED: return "COLOR_RED"; case COLOR_YELLOW: return "COLOR_YELLOW" Case COLOR_BLUE: return "COLOR_BLUE"; default: throw new ColorException ("Can't find this color");}} try {var color = "green"; / / green throws an exception var colorName = getConstantName (color);} catch (e) {var colorName = "unknown"; console.log (e.message, e.name); / / pass exception object to error handling}
The value of Symbol is unique, so the same worthy constant does not appear, which ensures that switch executes as expected by the code.
This is the end of the article on "what is the role of symbol in es6". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the role of 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.