当前位置:网站首页>LayaBox---TypeScript---Symbols
LayaBox---TypeScript---Symbols
2022-08-02 10:01:00 【格拉格拉】
目录
1.介绍
自ECMAScript 2015起,symbol
成为了一种新的原生类型,就像number
和string
一样。
symbol
类型的值是通过Symbol
构造函数创建的。
let sym1 = Symbol();
let sym2 = Symbol("key"); // 可选的字符串key
Symbols是不可改变且唯一的。
let sym2 = Symbol("key");
let sym3 = Symbol("key");
sym2 === sym3; // false, symbols是唯一的
像字符串一样,symbols也可以被用做对象属性的键。
let sym = Symbol();
let obj = {
[sym]: "value"
};
console.log(obj[sym]); // "value"
Symbols也可以与计算出的属性名声明相结合来声明对象的属性和类成员。
const getClassNameSymbol = Symbol();
class C {
[getClassNameSymbol](){
return "C";
}
}
let c = new C();
let className = c[getClassNameSymbol](); // "C"
2.众所周知的Symbols
Symbol.hasInstance方法,会被instanceof运算符调用。 构造器对象用来识别一个对象是否是其实例。
Symbol.isConcatSpreadable布尔值,表示当在一个对象上调用Array.prototype.concat时,这个对象的数组元素是否可展开。
Symbol.iterator方法,被for-of语句调用。返回对象的默认迭代器。
Symbol.match方法,被String.prototype.match调用。正则表达式用来匹配字符串。
Symbol.replace方法,被String.prototype.replace调用,正则表达式用来替换字符串中匹配的子串
Symbol.search方法,被String.prototype.search调用,正则表达式返回被匹配部分在字符串中的索引。
Symbol.species函数值,为一个构造函数。用来创建派生对象。
Symbol.split方法,被String.prototype.split调用。正则表达式用来分割字符串。
Symbol.toPrimitive方法,被ToPrimitive抽象操作调用,把对象转换为相应的原始值。
Symbol.toStringTag方法,被内置方法Object.prototype.toString调用。返回创建对象时默认的字符串描述。
Symbol.unscopables对象,它自己拥有的属性会被with作用域排除在外。
边栏推荐
- 周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
- 一文带你了解推荐系统常用模型及框架
- 基于列表的排队与叫号系统
- R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data
- R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
- 牛客网项目17节生成验证码 刷新验证码一直没反应
- 【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
- Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
- MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
猜你喜欢
The k-nearest neighbor method in the notes of Li Hang's "Statistical Learning Methods"
Facebook自动化数据分析方案,广告投放省心省力
关于缓存数据的探讨
软件测试与质量 之白盒测试
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
超赞!发现一个APP逆向神器!
Use the scrapy to climb to save data to mysql to prevent repetition
你好,我的新名字叫“铜锁/Tongsuo”
斯皮尔曼相关系数
随机推荐
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
软件测试H模型
转转反爬攻防战
ConvNeXt论文及实现
二维数组零碎知识梳理
The realization of the list
云原生应用平台的核心模块有哪些
The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
Facebook's automated data analysis solution saves worry and effort in advertising
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
Rear tube implements breadcrumb function
Unknown content monitoring
Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes
Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning
armv7与armv8的区别(v8和w12的区别)
QT专题:事件机制event基础篇
Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
【技术分享】OSPFv3基本原理