首先先熟悉一下Symbol类型的定义及其做用:javascript
内置的Symbol值主要是用于ECMAScript规范算法扩展的。本文主要是经过对规范的解读来了解Symbol内置的值是如何使用及其规范定义的。java
咱们先浏览一下规范里的Symbol内置的值:es6
规范名称 | Description | 值及其做用 |
---|---|---|
@@asyncIterator | "Symbol.asyncIterator" | 一个返回异步迭代器的方法,主要用于for await |
@@hasInstance | "Symbol.hasInstance" | 用于确认对象是否为该构造函数实例的方法,主要用于instanceof |
@@isConcatSpreadable | "Symbol.isConcatSpreadable" | 一个Boolean值,标识是否能够经过Array.prototype.concat进行扁平化处理 |
@@iterator | "Symbol.iterator" | 一个返回异步迭代器的方法,主要用于for of |
@@match | "Symbol.match" | 用于String.prototype.match调用 |
@@replace | "Symbol.replace" | 用于String.prototype.replace调用 |
@@search | "Symbol.search" | 用于String.prototype.search调用 |
@@species | "Symbol.species" | 一个用来返回建立派生对象的构造函数的方法 |
@@split | "Symbol.split" | 用于String.prototype.split调用 |
@@toPrimitive | "Symbol.toPrimitive" | 用于ToPrimitive抽象方法 |
@@toStringTag | "Symbol.toStringTag" | 用于描述一个对象的字符串,主要用于Object.prototype.toString调用 |
@@unscopables | "Symbol.unscopables" | 用于with环境绑定中排除的属性名称 |
上面有些描述比较抽象,不要急,咱们将逐个来仔细了解其规范定义和做用算法
和上面描述的同样,用于确认对象是否为该构造函数实例的方法,主要用于instanceof,当调用instanceof时,内部方法会调用对象上的Symbol.hasInstance方法。typescript
咱们来看一个例子json
class MyArray { static [Symbol.hasInstance](val){ return val instanceof Array; } } [1,2,3] instanceof MyArray; // true
在执行instanceof (V instanceof target) 操做时,Es6规范规定如下步骤:api
<font color="red">注意:这里会将结果值进行隐式转换</font>数组
紧接着咱们看一下OrdinaryHasInstance是怎么规定的:promise
判断是否有[[BoundTargetFunction]]内部属性,若是有, let BC = target.[[BoundTargetFunction]],返回 V instanceof BC, 算法结束。app
<font color="red">注意: 这里的[[BoundTargetFunction]]实际上是调用bind方法以前的原始方法</font>
看下面的例子说明:
function F1(){} const F2 = F1.bind({}); const obj = new F2(); obj instanceof F1 // true
let V = V.__proto__; if (V === null) { return false; } if(P === V){ return true; }
Function.prototype[@@hasInstance] = function(V) { return OrdinaryHasInstance(this, V); }
咱们能够看到在es6规范中,先尝试获取对象上的@@hasInstance方法,若是有,先调用对象上的@@hasInstance方法并返回。
@@isConcatSpreadable用于在执行Array.prototype.concat时判断对象是否可展开。
咱们先看两个例子
class MyArray { constructor(){ this.length = 0; } push(val){ this[this.length++] = val; } [Symbol.isConcatSpreadable] = true; } const array = new MyArray(); array.push(1); array.push(2); Array.prototype.concat.call(array, []); //[1,2] 这里自动展开array [].concat(array); // [1,2] 这里自动展开array class MyArrayNotConcatSpreadable { constructor(){ this.length = 0; } push(val){ this[this.length++] = val; } } const array2 = new MyArrayNotConcatSpreadable(); array2.push(1); array2.push(2); [].concat(array2); // [MyArrayNotConcatSpreadable对象] 这里不会自动展开array2
@@isConcatSpreadable用于IsConcatSpreadable抽象方法,先看一下IsConcatSpreadable(O)规范定义:
IsConcatSpreadable是抽象方法,不会暴露给javascript api,仅供内部调用,其用于Array.prototype.concat方法。
IsConcatSpreadable在Array.prototype.concat中会产生以下做用:
伪代码以下
const O = ToObject(this.value); const A = ArraySpeciesCreate(O, 0); let n = 0; for(item of [O, ...arguments]){ if(IsConcatSpreadable(item)){ const length = item.length; let k = 0; while(k < length) { if(item.HasProperty(ToString(k))){ Object.defineProperty(A, ToString(k), { value: item[ToString(k)] }); } } } }
注意:上述伪代码只是展现了IsConcatSpreadable的使用,并非所有的concat算法逻辑
@@match主要用于两个地方
咱们仍是结合例子看:
const helloWorldStartMatcher = { toString(){ return 'Hello'; } } 'Hello World'.startsWith(helloWorldStartMatcher);// true // startsWith在这里会调用helloWorldStartMatcher的toString方法进行判断 helloWorldStartMatcher[Symbol.match] = function(){ return true; } 'Hello World'.startsWith(helloWorldStartMatcher);// throw TypeError // startsWith调用时会调用IsRegExp对helloWorldStartMatcher进行判断,由于定义了Symbol.match,全部返回true,startsWith会对正则抛出TypeError
const helloWorldMatcher = { [Symbol.match](val){ return 'Hello World'.indexOf(val); } } 'Hello'.match(helloWorldMatcher); // 0 helloWorldMatcher[Symbol.match] = function(){ return /Hello/[Symbol.match](val); }; 'Hello World'.match(helloWorldMatcher); // 执行正则的match逻辑 等同于 'Hello World'.match(/Hello/);
IsRegExp(argument)规范定义以下:
IsRegExp主要用于String.prototype.startsWith和String.prototype.endsWith,在这两个方法中会先经过IsRegExp对参数进行判断,若是是true,会抛出typeError异常。
@@match被String.prototype.match ( regexp )调用规则以下:
注意:上述描述只是展现了@@match在规范中的做用,并非所有的String.prototype.match算法逻辑
@@replace用于String.prototype.replace,自定义replace逻辑
例子
const upperCaseReplacer = { [Symbol.replace](target, replaceValue){ return target.replace('hello', replaceValue.toUpperCase()); } } 'hello world'.replace(upperCaseReplacer, 'my');// MY world
@@replace被String.prototype.replace ( searchValue, replaceValue )调用规则以下:
注意:上述描述只是展现了@@replace在规范中的做用,并非所有的String.prototype.replace算法逻辑
@@search用于String.prototype.search,自定义search逻辑
例子
const upperCaseSearcher = { value: '', [Symbol.search](target){ return target.search(this.value.toUpperCase()); } } upperCaseSearcher.value = 'world'; 'hello WORLD'.search(upperCaseSearcher);// 6
@@search被String.prototype.search (regexp)调用规则以下:
注意:上述描述只是展现了@@search在规范中的做用,并非所有的String.prototype.search算法逻辑
@@split用于String.prototype.split,自定义split逻辑
例子
const upperCaseSplitter = { value: '', [Symbol.split](target, limit){ return target.split(this.value.toUpperCase(), limit); } } upperCaseSplitter.value = 'world'; 'hello WORLD !'.split(upperCaseSplitter);// ["hello ", " !"] 'hello WORLD !'.split(upperCaseSplitter, 1);// ["hello "]
@@split被String.prototype.split ( separator, limit )调用规则以下:
注意:上述描述只是展现了@@split在规范中的做用,并非所有的String.prototype.split算法逻辑
@@toStringTag经过Object.prototype.toString来调用的,用于描述对象。
例子
const obj = { [Symbol.toStringTag]: 'Hello' } Object.prototype.toString.call(obj); // "[object Hello]" class ValidatorClass {} Object.prototype.toString.call(new ValidatorClass()); // "[object Object]" 默认值 class ValidatorClass { get [Symbol.toStringTag]() { return "Validator"; } } Object.prototype.toString.call(new ValidatorClass()); // "[object Validator]" class ValidatorClass { get [Symbol.toStringTag]() { return {}; } } Object.prototype.toString.call(new ValidatorClass()); // "[object Object]"
@@toStringTag被Object.prototype.toString调用规则以下:
Es6新增的@@toStringTag以下:
对象 | 值 |
---|---|
Atomics | Atomics |
Math | Math |
JSON | JSON |
Symbol.prototype | Symbol |
Map.prototype | Map |
Set.prototype | Set |
WeakMap.prototype | WeakMap |
WeakSet.prototype | WeakSet |
Promise.prototype | Promise |
ArrayBuffer.prototype | ArrayBuffer |
Module Namespace Objects | Module |
SharedArrayBuffer.prototype | SharedArrayBuffer |
DataView.prototype | DataView |
GeneratorFunction.prototype | GeneratorFunction |
AsyncGeneratorFunction.prototype | AsyncGeneratorFunction |
Generator.prototype | Generator |
AsyncGenerator.prototype | AsyncGenerator |
AsyncFunction.prototype | AsyncFunction |
%StringIteratorPrototype% | String Iterator |
%ArrayIteratorPrototype% | Array Iterator |
%MapIteratorPrototype% | Map Iterator (new Map()[Symbol.iterator]()) |
%SetIteratorPrototype% | Set Iterator |
%AsyncFromSyncIteratorPrototype% | Async-from-Sync Iterator |
@@toPrimitive被ToPrimitive抽象方法调用,主要做用于类型转换。
咱们仍是结合例子来看:
const obj = { [Symbol.toPrimitive](hint){ if(hint === 'number') { return 2; } return '1'; } } const keyObj = { '1': 1 }; console.log(1 - obj);// -1 调用ToNumber类型转换 console.log(1 == obj); // true 抽象相等算法时调用 console.log(obj + 1); // 11 +号操做符时调用 console.log(keyObj[obj]); // 调用ToPropertyKey进行转换 console.log(0 < obj); // 抽象比较算法时调用 obj[Symbol.toPrimitive] = function(){return '2017-05-31'}; console.log(new Date(obj)); // Date构造时调用 obj[Symbol.toPrimitive] = function(){return {}}; console.log(obj + 1);// throw type error
因为ToPrimitive抽象方法是Es6底层最主要的抽象方法之一,调用点比较多,咱们先注重看一下它的实现。
ToPrimitive ( input [ , PreferredType ] )被定义为以下:
let exoticToPrim = GetMethod(input, @@toPrimitive),若是exoticToPrim不是undefined进行以下操做
OrdinaryToPrimitive为Es5规范定义的ToPrimitive方法,这里顺带介绍一下:
其次咱们看一下ToPrimitive调用点:
在es规范中,不少的方法都须要获取当前调用者的构造函数,而后根据此构造函数构造对象,可能这样说比较抽象,咱们仍是先看例子吧。
class MyArray extends Array{ } const array = new MyArray(); array.push(1); array.push(2); console.log(array instanceof Array); // true console.log(array instanceof MyArray); // true const mapArray = array.map(item => item); console.log(mapArray instanceof Array); // true console.log(mapArray instanceof MyArray); // true
从上面的例子中咱们看到,map后的数组仍是经过MyArray构造的,有时咱们但愿建立衍生对象时使用咱们指定的构造器。
class MyArray extends Array{ static [Symbol.species] = Array; // 等同于上面效果 //static get [Symbol.species](){ // return Array; //} } const array = new MyArray(); array.push(1); array.push(2); console.log(array instanceof Array); // true console.log(array instanceof MyArray); // true const mapArray = array.map(item => item); console.log(mapArray instanceof Array); // true console.log(mapArray instanceof MyArray); // false
在es6规范中,Symbol.species扩展属性主要做用于两个抽象动做中,分别是SpeciesConstructor,ArraySpeciesCreate,咱们先来看看这两个抽象动做具体是如何执行的。
SpeciesConstructor ( O, defaultConstructor )定义以下:
其中O是当前的调用者,若是O中不存在@@species属性就以defaultConstructor为默认构造器
ArraySpeciesCreate ( originalArray, length )定义以下:
其中originalArray是当前的调用数组
若是C是Object
注:上述是规范的简化过程,去除了一些断言和判断
咱们看一下SpeciesConstructor调用点:
例如
class MyPromise extends Promise { } const thenMyPromise = MyPromise.resolve().then(); console.log(thenMyPromise instanceof MyPromise); // true console.log(thenMyPromise instanceof Promise); // true class MyPromise2 extends Promise { static get [Symbol.species]() { return Promise; } } const thenMyPromise2 = MyPromise2.resolve().then(); console.log(thenMyPromise2 instanceof MyPromise); // false console.log(thenMyPromise2 instanceof Promise); // true
ArraySpeciesCreate调用点:
主要用于Array原型上的方法时调用触发,包括concat, filter, flat,map,slice,splice方法
es6规范中定义的javascript原始类型的@@species默认值为 Return the this value.
这个多是自定义时使用的最多的,它能够帮助咱们自定义迭代器,并且ECMAScript规范中的Set,Map等迭代过程都是基于它实现的。
在Typescript的Es6签名库,咱们能够看到迭代器的签名以下:
interface IteratorReturnResult<TReturn> { done: true; value: TReturn; } interface IteratorYieldResult<TYield> { done?: false; value: TYield; } type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>; interface Iterator<T, TReturn = any, TNext = undefined> { next(...args: [] | [TNext]): IteratorResult<T, TReturn>; return?(value?: TReturn): IteratorResult<T, TReturn>; throw?(e?: any): IteratorResult<T, TReturn>; } interface Iterable<T> { [Symbol.iterator](): Iterator<T>; }
经过签名咱们能够看到实现自定义迭代器须要扩展[Symbol.iterator]方法,而该方法要返回一个Iterator,Iterator中的next方法接受一个值,返回IteratorResult。其中的return方法的使用场合是,若是for...of循环提早退出(一般是由于出错,或者有break语句),就会调用return方法。
throw方法,能够在函数体外抛出错误,而后在 Generator 函数体内捕获,主要是配合Generator使用。
咱们先看两个例子感觉一下。
function *iterable () { yield 1; yield 2; yield 3; }; // iterable()返回一个迭代器 for(const val of iterable()){ console.log(val); // 输出1,2,3 } class EvenArray extends Array { [Symbol.iterator](){ const _this = this; let index = 0; return { next(){ if(index < _this.length){ const value = _this[index]; index += 2; return { done: false, value, } } return { done: true }; }, return() { this._index = 0; console.log('return iterator'); return { done: true } } } } } const array = new EvenArray(); for(let i = 0; i <= 100; i++){ array.push(i); } for(const val of array){ console.log(val); // 0, 2, 4, 6, ... , 98, 100 } for(const val of array){ console.log(val); // 0 // return iterator 调用了return 方法 break; } for(const val of array){ console.log(val); // 0 // return iterator 调用了return 方法 throw new Error(); } // //等同于上面代码 // class EvenArray extends Array { // constructor(){ // super(); // this.index = 0; // } // [Symbol.iterator](){ // this.index = 0; // return this; // } // next(){ // if(this.index < this.length){ // const value = this[this.index]; // this.index += 2; // return { // done: false, // value, // } // } // return { // done: true // }; // } // } const myIterable = {} myIterable[Symbol.iterator] = function* () { yield 1; yield 2; yield 3; }; // 扩展默认调用迭代器 console.log([...myIterable]); // [1, 2, 3] function *iterable2 () { yield* myIterable; //悬停myIterable迭代器 }; for(const val of iterable2()){ console.log(val); // 1,2,3 } function consoleArgs(...args){ console.log(args); } consoleArgs(...myIterable);// 剩余参数调用默认调用迭代器
先梳理一下@@iterator的调用点:
咱们一个个来剖析里面的具体实现及其做用
GetIterator ( obj [ , hint [ , method ] ] )定义以下
若是method没有提供,进行以下操做
若是hint为async
若是method为undefined,则
经过上述算法咱们能够看到,GetIterator最终返回一个包装好的迭代器对象。那么都有那些地方调用GetIterator抽象方法呢?
因为迭代器涉及的调用点比较多,可能须要单独的一篇文档介绍,这里注重看一下for of的规范:
for of执行主要包含两个部分:
接下来看一下ForIn/OfHeadEvaluation(TDZnames, expr, iterationKind )的规范定义:
说明:该抽象方法有三个参数,分别表示:绑定的环境变量名称、of后面的语句、迭代的类型(包括enumerate、async-iterate、iterate)。具体含义及其做用咱们接着往下看。
若是TDZnames不为空,执行以下操做
判断iterationKind是否为enumerate,若是是(这里主要用于for in)
不然
上述方法返回的结果会传入到ForIn/OfBodyEvaluation进行变量执行
ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet [ , iteratorKind ])规范定义以下:
参数比较多,咱们一个一个解释:
算法执行逻辑以下:
开始进入循环
上述算法去除了规范里的一些繁琐的步骤,尤为是lhs解析绑定的部分,若是想要深刻了解,建议查看ECMAScript规范文档。
Es6内置的多数对象都实现来迭代器,具体以下:
Symbol.asyncIterator指定了一个对象的默认异步迭代器。若是一个对象设置了这个属性,它就是异步可迭代对象,可用于for await...of循环。
接下来咱们看几个例子:
const myAsyncIterable = new Object(); myAsyncIterable[Symbol.asyncIterator] = async function*() { yield 1; yield 2; yield 3; }; (async () => { for await (const x of myAsyncIterable) { console.log(x); // 输出: // 1 // 2 // 3 } })();
固然也能够经过它遍历promise
const myAsyncIterable = new Object(); const promise1 = new Promise(resolve=>setTimeout(() => resolve(1), 500)); const promise2 = Promise.resolve(2); myAsyncIterable[Symbol.asyncIterator] = async function*() { yield await promise1; yield await promise2; }; (async () => { for await (const x of myAsyncIterable) { console.log(x); // 输出: // 1 // 2 } })();
也能够自定义异步迭代器
const myAsyncIterable = { promiseList:[ new Promise(resolve=>setTimeout(() => resolve(1), 500)), Promise.resolve(2) ], [Symbol.asyncIterator](){ const _this = this; let index = 0; return { next(){ if(index === _this.promiseList.length){ return Promise.resolve({done: true}); } return _this.promiseList[index++].then(value => ({done: false, value})) } } } }; (async () => { for await (const x of myAsyncIterable) { console.log(x); // 输出: // 1 // 2 } })();
@@asyncIterator做用和@@iterator,在规范定义中也是统一处理的,只是在执行ForIn/OfBodyEvaluation时iteratorKind参数设置为了async,执行函数时经过Await动做处理@@asyncIterator。
对象的Symbol.unscopables属性,指向一个对象。该对象指定了使用with关键字时,哪些属性会被with环境排除
const object1 = { property1: 42 }; object1[Symbol.unscopables] = { property1: true }; with (object1) { console.log(property1); // expected output: Error: property1 is not defined }
@@unscopables用于HasBinding调用
HasBinding查看对象是否绑定到当前的环境记录项中,规范中的HasBinding最后会经过@@unscopables进行过滤。
规范中只有Array.prototype指定了@@unscopables
具体以下:
{ "copyWithin":true, "entries":true, "fill":true, "find":true, "findIndex":true, "flat":true, "flatMap":true, "includes":true, "keys":true, "values":true }