Symbol

作用:用来表示一个独一无二的值:

console.log(new Symbol() === new Symbol());// false
console.log(new Symbol('f') === new Symbol('f'));// false

可以作为对象的属性值存在:

const name = new Symbol();
const obj = {
	[name ]:'xxx', //私有属性
	say() { console.log(this[name]); }
}
Object.getOwnPropertySymbols(obj); //获取Symbol属性名

复用相同的symbol值

const a = Symbol.for('f');
const b = Symbol.for('f');  //该方法维护了字符串和Symbol的注册表
console.log(a === b); // true
const c = Symbol.for(true);
const d = Symbol.for('true');
console.log(c === d); //true

属性

  • Symbol.asyncIterator 符号指定了一个对象的默认异步迭代器。如果一个对象设置了这个属性,它就是异步可迭代对象,可用于for await…of循环。
  • Symbol.hasInstance 用于判断某对象是否为某构造器的实例。因此你可以用它自定义 instanceof 操作符在某个类上的行为。
  • Symbol.isConcatSpreadable符号用于配置某对象作为Array.prototype.concat()方法的参数时是否展开其数组元素。
  • Symbol.iterator 为每一个对象定义了默认的迭代器。该迭代器可以被 for…of 循环使用。
  • Symbol.match 指定了匹配的是正则表达式而不是字符串。String.prototype.match() 方法会调用此函数。
  • Symbol.matchAll 返回一个迭代器,该迭代器根据字符串生成正则表达式的匹配项。此函数可以被 String.prototype.matchAll() 方法调用。
  • Symbol.replace 这个属性指定了当一个字符串替换所匹配字符串时所调用的方法。String.prototype.replace() 方法会调用此方法。
  • Symbol.search 指定了一个搜索方法,这个方法接受用户输入的正则表达式,返回该正则表达式在字符串中匹配到的下标,这个方法由以下的方法来调用 String.prototype.search()。
  • Symbol.species 是个函数值属性,其被构造函数用以创建派生对象。
  • Symbol.split 指向 一个正则表达式的索引处分割字符串的方法。 这个方法通过 String.prototype.split() 调用。
  • Symbol.toPrimitive 是一个内置的 Symbol 值,它是作为对象的函数值属性存在的,当一个对象转换为对应的原始值时,会调用此函数。
  • Symbol.toStringTag 是一个内置 symbol,它通常作为对象的属性键使用,对应的属性值应该为字符串类型,这个字符串用来表示该对象的自定义类型标签,通常只有内置的 Object.prototype.toString() 方法会去读取这个标签并把它包含在自己的返回值里。
  • Symbol.unscopables 指用于指定对象值,其对象自身和继承的从关联对象的 with 环境绑定中排除的属性名称。
### Symbol in Programming and IT Context In the programming and IT context, a symbol represents an object or entity within code that can be referenced by name. Symbols are fundamental components used across various aspects of software development and system architecture. For instance, when working with binary analysis tools like Angr, symbols refer to named entities defined during compilation such as functions or global variables[^1]. These symbols allow developers to interact more meaningfully with compiled binaries without needing detailed knowledge about internal memory addresses: ```python sym = project.loader.find_symbol('missing_symbol') print(sym) # This command retrieves information regarding 'missing_symbol' ``` Symbols play crucial roles not only in low-level operations but also high-level abstractions including databases where they might represent keys or columns serving specific purposes[^3]. #### Characteristics of Symbols - **Uniqueness**: Within certain scopes, each symbol should have unique identifiers. - **Referential Integrity**: Ensures relationships between different parts remain consistent throughout application lifecycle. - **Descriptive Naming**: Helps improve readability and maintainability of source codes. #### Applications Across Domains Beyond direct coding practices, understanding symbols enhances capabilities in multiple domains: - **Binary Analysis & Reverse Engineering** - **Database Management Systems (DBMS)** - **Compiler Design** By grasping this concept thoroughly, one gains deeper insights into how programs operate internally while facilitating effective debugging sessions alongside enhancing overall productivity.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值