In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Symbol in ECMAScript 6". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use Symbol in ECMAScript 6".
The ES6 data type adds Symbol in addition to Number, String, Boolean, Objec t, 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
Working with scen
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.
Let sy = Symbol ("key1"); / / 1let syObject = {}; syObject [sy] = "kk"; console.log (syObject); / / {Symbol (key1): "kk"} / / 2let syObject = {[sy]: "kk"}; console.log (syObject); / / {Symbol (key1): "kk"} / / 3let syObject = {}; Object.defineProperty (syObject, sy, {value: "kk"}) Console.log (syObject); / / {Symbol (key1): "kk"}
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:
Const COLOR_RED = "red"; const COLOR_YELLOW = "yellow"; const COLOR_BLUE = "blue"; const MY_BLUE = "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"; case MY_BLUE: return "MY_BLUE"; default: throw new ColorException ("Can't find this color");}} try {var color = "green"; / / green throws exception var colorName = getConstantName (color);} catch (e) {var colorName = "unknown" Console.log (e.message, e.name); / / pass exception object to error handling}
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.
Symbol.for ()
Symbol.for () is similar to the singleton pattern, which first searches globally for whether the string parameter is the Symbol value of the name in the registered Symbol. If so, the Symbol value is returned, if not, a Symbol value with the name of the string parameter is created and returned, and registered in the global environment for search.
Let yellow = Symbol ("Yellow"); let yellow1 = Symbol.for ("Yellow"); yellow = = yellow1; / / false let yellow2 = Symbol.for ("Yellow"); yellow1 = yellow2; / / true
Symbol.keyFor ()
Symbol.keyFor () returns the key of a registered Symbol type value, which is used to detect whether the string parameter has been registered as the Symbol value of the name.
Let yellow1 = Symbol.for ("Yellow"); Symbol.keyFor (yellow1); / / "Yellow" is all the content of this article "how to use Symbol in ECMAScript 6". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.