当前位置:网站首页>什么?你还不知道Symbol?
什么?你还不知道Symbol?
2022-07-15 11:59:00 【InfoQ】
前言
SymbolSymbol的特点
- Symbol的值是唯一的,用来解决命名冲突的问题
- Symbol值不能与其他数据进行运算
- Symbol定义的对象属性不能使用
for...in循环遍历,但是可以使用Reflect.ownKeys来获取对象的所有键名
创建Symbol的两种方式
创建Symbol
// 创建Symbol
let s = Symbol();
console.log(s, typeof s); // Symbol() 'symbol'
s let s2 = Symbol('Ned');
let s3 = Symbol('Ned');
console.log(s2 === s3); // false
使用Symbol.for创建Symbol
let s4 = Symbol.for('Ned');
console.log(s4, typeof s4); // Symbol(Ned) 'symbol'
Symbol.for let s4 = Symbol.for('Ned');
let s5 = Symbol.for('Ned');
console.log(s4 === s5); // true
【强调】Symbol值不能与其他数据进行运算
let a = Symbol();
let b = a + 100;
let c = a + '100';
Symbol的基本使用
let game = {
name: '石头剪刀布',
...
up: function() {
console.log('这是up函数');
},
down: function() {
console.log('这是down函数');
}
...
}
gameupdownupdownSymbol // 声明一个method对象
let methods = {
up: Symbol(),
down: Symbol()
}
// 向game对象中,注入方法
game[methods.up] = function(){
console.log('我要上升!');
}
game[methods.down] = function(){
console.log('我要下降!');
}
gamegamegame name: "石头剪刀布"
down: f()
up: f()
Symbol(): f()
Symbol(): f()
数据类型小诀窍
最后
边栏推荐
- Google recommends using sealed and remotemediator in projects
- 02 stm32cubemx new project
- Nacos as the registration and discovery center and configuration center of microservice architecture
- Google recommends using kotlin flow in MVVM architecture
- 1. OLED simple drive
- HCIP笔记(1)
- 加密市场的牛熊周期;NFT 定义的争论
- How can im make enterprise telecommuting simpler and more efficient?
- Alibaba cloud Internet of things platform construction
- RobotFramework进阶(二)集成Pycharm及Api自动化用例编写
猜你喜欢

仓储系统亮灯分拣

解决中小型机房动环状态综合监控方案

Using PostgreSQL on PostgreSQL 15_ FDW performs parallel commit of transactions

Envoyez votre code dans l'espace et développez "le plus grand travail" avec Huawei Cloud

Unit MySQL appears in MySQL Solution of service could not be found

A --- DMA serial port communication

Wechat applet uni app uses uni Getuserprofile to realize wechat authorized login (with source code)

Getting started with esp8266 Lua

HCIP回顾(2)

Hcip review (1)
随机推荐
箭头函数与箭头函数的区别
Hcip notes (1)
Configuration of teacher management module and MP automatic code generation
Hcip review (2)
基础概念
第九十八期:Flutter学习(一)
Mqtt--- subscribe and suback
Relevant knowledge points
RobotFramework进阶(三)集成Jenkins运行自动化用例
“代码写的越急,程序跑的越慢”
Efficient development of harmonyos course applications based on ETS
金融市场暴跌忍不住想抄底?劝你看完本文再做决定
Meta宣布推出Make-A-Scene:可基于文字和草图控制AI图像生成
元宇宙大爆发是谁在“跑马圈地”?
XPath actual combat: climbing the learning ape land (Part 1)
Interface development does not need to write controller, service, Dao, mapper, XML, VO, which is automatically generated!
HCIP笔记(2)
基于eTS高效开发HarmonyOS课程类应用
有奖调研 | openEuler 开发者体验调研问卷
Adding an index by Oracle causes other users to lose their query permissions on this table