当前位置:网站首页>ES6 symbol explanation
ES6 symbol explanation
2022-06-09 03:42:00 【zhulin1028】
ES6 A new type of raw data is introduced Symbol, Represents a unique value .
ES5 Object property names are strings, which easily cause property name conflicts .
var a = { name: 'lucy'};
a.name = 'lili'; This will override the properties ES6 A new type of raw data is introduced Symbol, Represents a unique value .
Review new knowledge : The basic data types are 6 Kind of :Undefined、Null、 Boolean value (Boolean)、 character string (String)、 The number (Number)、 object (Object).
Here a new :Symbol
Be careful ,Symbol Cannot use before function new command , Otherwise, an error will be reported . This is because of the creation of Symbol Is a primitive type of value , Not object
Symbol Function can take a string as an argument , Said to Symbol Description of the example , Mainly for display on the console , Or to string , Easy to distinguish .
// Without parameters
var s1 = Symbol();
var s2 = Symbol();
s1 === s2 // false
// With parameters
var s1 = Symbol("foo");
var s2 = Symbol("foo");
s1 === s2 // falseSymbol Value cannot be evaluated with other types of values
As the property name Symbol
var mySymbol = Symbol();
// The first way to write it
var a = {};
a[mySymbol] = 'Hello!';
// The second way
var a = {
[mySymbol]: 'Hello!'
};
// The third way
var a = {};
Object.defineProperty(a, mySymbol, { value: 'Hello!' });
// All of the above results are the same
a[mySymbol] // "Hello!"Be careful ,Symbol Value as object property name , You can't use the dot operator .
var a = {};
var name = Symbol();
a.name = 'lili';
a[name] = 'lucy';
console.log(a.name,a[name]);//lili,lucySymbol Value as property name , This property is also a public property , It's not a private property .
This is a bit like java Medium protected attribute (protected and private The difference between : It is inaccessible outside the class , Subclasses within a class can inherit protected Cannot inherit private)
But here Symbol It is also accessible outside the class , It just won't show up in for...in、for...of In circulation , And not by Object.keys()、Object.getOwnPropertyNames() return . But there's one Object.getOwnPropertySymbols Method , You can get all of the Symbol Property name
Symbol.for(),Symbol.keyFor()
Symbol.for The mechanism is somewhat similar to the singleton pattern , First, search the global for any with this parameter as the name Symbol value , If there is , I'm just going to return this Symbol value , Otherwise, it will create and return a string named Symbol value . And direct Symbol It's a little different .
var s1 = Symbol.for('foo');
var s2 = Symbol.for('foo');
s1 === s2 // trueSymbol.keyFor Method returns a registered Symbol The type is worth key. The essence is to detect the Symbol Created or not
var s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"
var s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined边栏推荐
- word+正则表达式==快速批量添加图注题注(保姆级图文)
- 『C语言』面试管:说下各个字符串函数功能,我:不知道。面试官:小伙子学会了再来面试吧。
- 服务器注册使用
- Concurrent operation BlockingQueue
- SQL audit | here are the most commonly used SQL development rules for mysql/oracle
- 网页设计期末大作业-景点旅游网站(含导航栏,轮播图,样式精美)
- Development of DAPP system for digital currency holding interest bearing pledge financial management
- TypeScript的泛型和装饰器
- 印尼Widya Robotics携手华为云,让建筑工地安全看得见
- Failure analysis | xtrabackup backup failure caused by DDL
猜你喜欢

接口性能测试:web service接口测试

Free video format converter
![[share] network packet loss fault handling scheme](/img/40/39e6548651eaeec5e6c1c39651cc88.png)
[share] network packet loss fault handling scheme

故障分析 | MySQL 数据库升级后,数据库怎么卡住了

Failure analysis | xtrabackup backup failure caused by DDL

Fault analysis - a case of excessive CPU load caused by a large number of short-time processes

The key points of writing test cases for the first time

【分享】网络丢包故障处理方案

外网访问局域网方法和VM安装的虚拟机如何在局域网内互相访问

Final assignment of Web Design - scenic spot tourism website (including navigation bar, rotation map, exquisite style)
随机推荐
技术分享 | 调整 max-write-buffer-size 优化 pika 性能10倍的案例
Today in history: kubernetes open source version released; Worldofwarcraft landed in China; The inventor of packet switching network was born
汇编:jmp指令
[share] network packet loss fault handling scheme
The key points of writing test cases for the first time
汇编:Debug的常用命令
kubernetes 二进制安装(v1.20.16)(四)部署 master
专家,如何快速转型管理者?
内存飙升问题定位
SQL audit | here are the most commonly used SQL development rules for mysql/oracle
SQL审核 | 这里有 MySQL/Oracle 最常用的 SQL 开发规则
Two merged sequences (CF 1144 g) -dp
Alook browser cookie acquisition tutorial
GC日志分析
华为云服务器装数据库远程连接被拒绝
解决MYSQL库or表进行删除等操作时出现卡死等情况
Traversal of binary tree
New Presto data source support, new symbol map, and release of dataease open source data visual analysis platform v1.11.0
No cached version available for offline mode
印尼Widya Robotics携手华为云,让建筑工地安全看得见