Section tenth, symbol seventh type of data

Source: Internet
Author: User
SymbolOverview as symbol instance of property name: Eliminates the traversal of the Magic string property name Symbol.for (), Symbol.keyfor () instance: the Symbol value built into the module's Singleton mode Overview

ES5 object property names are strings, which can easily cause property names to conflict. For example, if you use an object provided by another person but want to add a new method to the object (mixin mode), the name of the new method may conflict with the existing method. If there is a mechanism to ensure that the names of each attribute are unique, this will fundamentally prevent the conflict of property names. This is why ES6 introduced symbol.

ES6 introduces a new primitive data type symbol that represents a unique value. It is the seventh data type in the JavaScript language, the first six of which are: Undefined, Null, Boolean (Boolean), String (string), numeric (number), objects (object).

The symbol value is generated by the symbol function. This means that the object's property name can now have two types, one is the original string, and the other is the new symbol type. All property names that are of type symbol are unique and can be guaranteed to not conflict with other property names.

Let S = Symbol ();

typeof s
//"symbol"

In the code above, the variable s is a unique value. The result of the TypeOf operator indicates that the variable s is a symbol data type, not another type such as a string.

Note that the symbol function cannot be used before the new command, or an error is noted. This is because the generated symbol is a value of the original type, not an object. That is, the attribute cannot be added because the symbol value is not an object. Basically, it's a data type similar to a string.

The symbol function can accept a string as a parameter, representing a description of the symbol instance, primarily to make it easier to distinguish between a console display and a string conversion.

var S1 = Symbol (' foo ');
var s2 = Symbol (' bar ');

S1//symbol (FOO)
s2/symbol (BAR)

s1.tostring ()//"Symbol (foo)"
s2.tostring ()//"Symbol (BAR)"

In the code above, S1 and S2 are two symbol values. If you do not add parameters, their output in the console is symbol (), which is not conducive to distinction. With the parameters, it is equal to add a description of them, the output can be distinguished, in the end is which value.

If the parameter of Symbol is an object, the object's ToString method is called, and it is converted to a string before a symbol value is generated.

Const OBJ = {
  toString () {return
    ' abc ';
  }
};
Const SYM = Symbol (obj);
SYM//Symbol (ABC)

Note that the parameter of the symbol function simply represents a description of the current symbol value, so the return value of the symbol function for the same parameter is not equal.

Case with no parameters
var s1 = Symbol ();
var s2 = Symbol ();

S1 = = S2//False

//with parameters in the case of
var S1 = Symbol (' foo ');
var s2 = Symbol (' foo ');

S1 = = S2//False

In the code above, S1 and S2 are the return values of the symbol functions, and the parameters are the same, but they are not equal.

The symbol value cannot be calculated with a value of another type, and an error is made.

var sym = symbol (' my Symbol ');

Your symbol is "+ sym
//typeerror:can ' t convert symbol to string
' your symbol is ${SYM} '
//Typeerror:can ' T convert symbol to string

However, the symbol value can be explicitly converted to a string.

var sym = symbol (' my Symbol ');

String (SYM)//' symbol (my symbol) '
sym.tostring ()//' symbol (my symbol) '

In addition, the symbol value can be converted to a Boolean value, but cannot be converted to a value.

var sym = Symbol ();
Boolean (SYM)//True
!sym  //False

if (sym) {
  //...
}

Number (SYM)//TypeError
Sym + 2//TypeError
symbol as the property name

Because each symbol value is unequal, this means that the symbol value can be used as an identifier for the object's property name to guarantee that no property of the same name will appear. This is useful for an object that is composed of multiple modules, preventing a key from being accidentally overwritten or overwritten.

var mysymbol = Symbol ();

The first type is the
var a = {};
A[mysymbol] = ' hello! ';

The second way of writing
var a = {
  [Mysymbol]: ' hello! '
};

The third type of the
var a = {};
Object.defineproperty (A, Mysymbol, {value: ' hello! '});

The above writing all get the same result
A[mysymbol]///"hello!"

The above code specifies the object's property name as a symbol value through the square bracket structure and the object.defineproperty.

Note that the symbol value cannot be used as a point operator when it is an object property name.

var mysymbol = Symbol ();
var a = {};

A.mysymbol = ' hello! ';
A[mysymbol]//undefined
a[' mysymbol '///' hello! '

In the code above, because the dot operator is always followed by a string, the value that Mysymbol is referred to as the identity name is not read, causing A's property name to be actually a string rather than a symbol value.

Similarly, when you use the symbol value to define an attribute within an object, the symbol value must be placed in square brackets.

Let S = Symbol ();

Let obj = {
  [s]: function (ARG) {...}
};

Obj[s] (123);

In the code above, if S is not placed in square brackets, the key of the property is the string s, not the symbol value represented by S.

With the enhanced object notation, the Obj object of the above code can be written more concisely.

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.