11, ES6 a new data type symbol

Source: Internet
Author: User
Symbol resolves property name conflicts for objects
Remember some of the data types we know.
String number Object undefined Null
Boolean
Now, one more type is symbol.
Let A=symbol ()
Console.log (a)//symbol ()
Console.log (typeof a)//symbol
The code above uses a symbol () function to create a variant of the symbol type print a gets the symbol (). It represents a unique value.
var a1=symbol ()
VAT A2=symbol ()
A1===a2//false
It can be seen that A1 and A2 are not equal.
The Symbol () function, which can accept parameters, even if the parameters are accepted, they are not equal
var a1=symbol ("abc")
VAT A2=symbol ("abc")
A1===a2//false
When the symbol value is used as the property name of the object, it is not possible to use the dot operator to get the corresponding value, in brackets, because the dot operator causes JA ascript to interpret the following property name as a string type instead of a symbol type
var name=symbol ()
var person={[age]:29}
Console.log (person.age)//undefined
Console.log (Person[age])//29
When the value of the symbol type is used as the property name, this property is not for...in ... and For...of...,object.keys () traverse to,
var person={[name]: "Monkey King", "Age": 500}
Object.keys (person)//age
for (var key in person) {
Console.log (Key)//age
}
To get a property of the symbol type, use the Getownpropertysymbol () function to get only the symbol type, return an array, and the array member is the attribute value of the symbol type.
var name=symbol ("name")
var age=symbol ("Age")
var person={
[Name]: "Monkey King",
[Age]:500,
"From": "Huaguoshan"
}
Object.getownpropertysymbol (person)
[Symbol (name), symbol (age)]
Reflect.ownkeys () function
Get all types of properties at once, whether string or symbol type
var person={
[Symbol ("name")]: "Pig",
"Age": 500
}
Reflect.ownkeys (person)
["Age", Symbol (name)]
symbol.for () function
According to the parameter name, go to the global environment to search whether there is a symbol value in the name of the parameter, and then return it, do not use this parameter to create a new symbol value
var a1=symbol.for ("Age")
var a2=symbol.for ("Age")
A1===a2//true
The symbol value created by Symbol.for () is registered in the Global Environment, symbol.for () to search, and the value created by symbol is not searchable with symbol.for ().
symbol.keyfor () function
Returns a key that is registered as a symbol value in the global environment. No, return to undefined. This symbol value is created by Symbol.for ().
var a=symbol.for ("name")
Symbol.keyfor (a)//name


var a=symbol ("name")
Symbol.keyfor (a)//undefined















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.