Is es6 symbol a basic type?

青灯夜游
Release: 2022-03-08 17:00:02
Original
2473 people have browsed it

es6 symbol belongs to the basic type. Symbol is a new basic data type introduced in es6, which represents a unique value; its function is to prevent attribute name conflicts and ensure that each attribute name in the object is unique.

Is es6 symbol a basic type?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Symbol is a basic data type, a new data type introduced in ES6.

The purpose is to prevent attribute name conflicts and ensure that each attribute name in the object is unique.

let s1 = Symbol('foo');
let s2 = Symbol('foo');

s1 === s2 // false
Copy after login

The Symbol type can have a string parameter representing a description of the Symbol instance. So two instances of the Symbol type with the same description are not equal.

How to use

let s = Symbol();

// 第一种写法
let a = {};
a[mySymbol] = 'Hello!';

// 第二种写法
let a = {
  [mySymbol]: 'Hello!'
};

// 第三种写法
let a = {};
Object.defineProperty(a, mySymbol, { value: 'Hello!' });

// 以上写法都得到同样结果
a[mySymbol] // "Hello!"
Copy after login

It can be seen that when using the Symbol type as the attribute name, you must use []. If not used, it represents the same string as the variable name as the attribute name.

Symbol is used as the attribute name. When traversing the object, the attribute will not appear in the for...in, for...of loop. , will not be returned by Object.keys(), Object.getOwnPropertyNames(), JSON.stringify()

However, it is not a private property. There is a Object.getOwnPropertySymbols() method that can get all the Symbol property names of the specified object. This method returns an array whose members are all Symbol values ​​used as property names of the current object.

How to use the same Symbol value

Symbol.for("bar") === Symbol.for("bar")
// true
Copy after login

Use the Symbol.for() method, and the variables created with the same parameters will have the same value. Because the Symbol variable created using this method will register the parameters globally. The parameters of the variables created by Symbol() will not be registered globally.

let s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"

let s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined
Copy after login

Use Symbol.keyFor()The name of the globally registered parameter can be found in a variable.

Symbol built-in attribute value

Symbol.hasInstance: When other objects use the instanceof operator, they will use the internal method pointed to by the attribute name. .

Symbol.isConcatSpreadable

Symbol.species

Symbol.match

Symbol.replace

Symbol.search

Symbol.split

Symbol .iterator

Symbol.toPrimitive

Symbol.toStringTag

Symbol.unscopables

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Is es6 symbol a basic type?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!