In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "Is the symbol in es6 a basic type?" The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "Is the symbol in es6 a basic type?"
ES6 symbol is a basic type. Symbol is a new primitive data type introduced in ES6 that represents unique values; its role is to prevent conflicts in attribute names and ensure that each attribute name in an object is unique.
Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Symbol is a primitive data type, a new data type introduced in ES6.
The goal is to prevent attribute name conflicts and ensure that each attribute name in an object is unique.
let s1 = Symbol('foo');let s2 = Symbol('foo');s1 === s2 // false
Symbol types can have a string parameter that represents a description of the Symbol instance. So two instances of Symbol type of the same description are not equal.
How to use let s = Symbol();//the first way to write let a = {};a[mySymbol] = 'Hello! ';//second way let a = { [mySymbol]: 'Hello! '};//third way let a = {};Object.defineProperty(a, mySymbol, { value: 'Hello! ' });//all above get the same result a[mySymbol] // "Hello! "
It can be seen that to use Symbol type as attribute name, you must use [] to be able to use it. If not used, it indicates the same string as the variable name when the attribute name is an attribute.
Symbol as an attribute name, when traversing the object, the attribute does not appear in the for... in、for... Of loop, also not returned by Object.keys(), Object.getOwnPropertyNames(), JSON.stringify()
However, it is also not a private property, and there is an Object.getOwnPropertySymbols() method that gets all Symbol property names for the specified object. This method returns an array of all Symbol values used as attribute names for the current object.
Symbol.for("bar") == Symbol.for("bar")// true
With the Symbol.for() method, variables created with the same parameter are the same value. Because Symbol variables created using this method register parameters globally. Parameters of variables created by Symbol() are not registered globally.
let s1 = Symbol.for("foo");Symbol.keyFor(s1) // "foo"let s2 = Symbol("foo");Symbol.keyFor(s2) // undefined
Using Symbol.keyFor(), you can find the name of the parameter registered globally.
Symbol built-in attribute values
Symbol.hasInstance: Other objects use the internal method pointed to by the attribute name when using the instanceof operator.
Symbol.isConcatSpreadable
Symbol.species
Symbol.match
Symbol.replace
Symbol.search
Symbol.split
Symbol.iterator
Symbol.toPrimitive
Symbol.toStringTag
Symbol.unscopables
The above is "es6 symbol belongs to the basic type?" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to 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.
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.