[ES2019] Represent Collision-free String Constants as Symbols in JavaScript
2024-10-06 14:01:31
ES2019 introduces the Symbol.prototype.description property. In this lesson, we'll learn why this property is useful and unlocks Symbols as appropriate to use to represent strings constants in JS. We'll investigate a use case for string constants and see why using Symbols prior to ES2019 was not appropriate. Then we'll see how using .description fixes that. Lastly, we'll learn why using Symbols may be better than strings for constants in certain use cases by examining what it means to be "collision free".
If we define a Symbol:
const sy = Symbol('A')
Before ES2019, if we console.log `sy`, we will always get `Symbol(A)`.
After ES2019 we can get the value of Symbol:
console.log(sy.description) // 'A'
And Symbol are collision-free which means:
Symbol('a') === Symbol('a') //false
最新文章
- ABP 索引
- 集成EaseUI报错 环信3.1.5,托入EaseUI后,系统文件报错
- c语言数据结构和算法库--cstl---王博--相关网站和博客
- 跨服务器之间的session共享
- iOS-NSThread使用
- c#中如何得到百分比数值
- HTML5+开发移动app-mui开发示例
- The List ADT
- iOS开发常用
- javascript模板引擎template.js使用
- display:inline-block 来解决盒子高度不一样,造成的盒子浮动
- Reactor
- 如何将div中的内容设置为空同时还要保留div本身
- Shell教程 之test命令
- 版本管理(二)之Git和GitHub的连接和使用
- iOS开发中FMDB的使用
- HTML5/CSS3实现五彩进度条应用
- grpc protobuf
- Facebook回应追踪无账号用户:源于网站插件漏洞
- JSON字符串转换为Map