-
不可使用
new,避免创建符号包装对象查看使用
new与不使用new创建的字符串的区别const string = "string"; const stringObj = new String('string'); console.log(typeof string); //string console.log(typeof stringObj); //object string.a = 123; console.log(string.a);//undefined stringObj.a = 123; console.log(stringObj.a);//123 -
Symbol.for使用像
Symbol("create")这样永远创建的是唯一标识。使用Symbol.for在全局注册表中搜索,如果不存在才会创建新的实例,否则返回存在的实例。console.log(Symbol('create') === Symbol('create')); //false console.log(Symbol.for('create') === Symbol.for('create')); //true也就是说,使用
Symbol.for方法创建的实例,都会放在一个全局的表中去记录他的键值对。同时生成值的方法对于固定的key生成的value一定是一样的。而直接通过Symbol('create')创建的实例,生成的value不会记录在全局的表中,相当于一个私有变量。 -
Symbol.keyFor在全局表中寻找对应
key的实例。使用Symbol('create')声明的实例使用该方法找不到。let created = Symbol('create'); console.log(Symbol.keyFor(created)); //undefined let createdFor = Symbol.for('create'); console.log(Symbol.keyFor(createdFor)); //create -
使用
getOwnPropertySymbols查找对应实例与
Object.getOwnPropertyName功能类似,返回数组中以Symbol作为key的数组。Object.getOwnPropertyName与Object.getOwnPropertySymbols返回的数组互斥。
-
Symbol.asyncIterator -
Symbol.hasInstanceSymbol.hasInstance可以判断某个实例的是否是某个函数的实例。该方法可以重写。class Father {} class Child extends Father {} let c = new Child(); console.log(Child[Symbol.hasInstance](c)); //true console.log(Father[Symbol.hasInstance](c)) //true class Father {} class Child extends Father { static [Symbol.hasInstance] () { return false; } } let c = new Child(); console.log(Child[Symbol.hasInstance](c)); //false console.log(Father[Symbol.hasInstance](c)) //true -
Symbol.species作为派生对象的构造函数。比如说对于
Array来说,map方法会构造一个新的Array返回。设置该方法就相当于设置了新的Array的构造函数。如下:function Test() { console.log("我是构造函数"); } class Baz extends Array { static get[Symbol.species]() { return Test; } } let baz = new Baz(5, 1, 2, 5, 78, 87, 8); let derivation = baz.map(v => v); console.log(baz); //Baz [ 5, 1, 2, 5, 78, 87, 8 ] console.log(baz instanceof Array); //true console.log(derivation instanceof Array); //false console.log(derivation instanceof Test); //true -
Symbol.toPrimitive对一个
class重新设置该方法,可以改变该classnew 出来的对象在隐式或则显式转化为Number、String的值。参考ECMAScript® 2022中的
7.1.1.1 OrdinaryToPrimitive (
obj,hint)The abstract operation OrdinaryToPrimitive takes arguments
obj(an Object) andhint(string or number). It performs the following steps when called:-
If
hintis string, thenLet
methodNamesbe « "toString", "valueOf". -
Else,
Let
methodNamesbe « "valueOf", "toString". -
For each element
nameofmethodNames, doa. Let
methodbe ? Get(obj,name).b. If IsCallable(
method) is true,then
i. Let
resultbe ? Call(method,obj).ii. If Type(
result) is not Object, returnresult. 4. Throw a TypeError exception.
class Test { constructor() { this[Symbol.toPrimitive] = function(hint) { console.log(hint); switch (hint) { case 'number': return -1; case 'string': return 'haha'; case 'default': default: return 'o'; } } } } let test = new Test(); Number(test) //-1 String(test) //haha -
-
Symbol.toStringTag用于创建对象的默认 字符串描述。由内置方法 Object.prototype.toString()使用
class Test { constructor() { this[Symbol.toStringTag] = "HEHE" } } let test = new Test(); console.log(test.toString()); console.log(test[Symbol.toStringTag]);