JavaScript 个生成器及其原型链
JavaScript generators and their prototype chains
我正在研究 JavaScript 生成器,根据以下代码及其输出有 2 个问题:
const a = function*(){}(); // Object [Generator] {}
const b = a.__proto__; // Object [Generator] {}
const c = b.__proto__; // Object [Generator] {}
const d = c.__proto__; // {}
const e = d.__proto__; // {}
const f = e.__proto__; // null
console.log(a, b, c, d, e, f);
问题 1
似乎每个生成器对象都有自己独特的原型,而且它们都有一个共同的原型:
const x = function*(){}();
const y = x.__proto__;
const z = y.__proto__;
console.log(b === y); // false
console.log(c === z); // true
我上面的理解对吗?
问题 2
因为f
是null
,e
可能是Object.prototype
:
console.log(e === Object.prototype); // true
但是,我想不通d
是什么。有没有等于d
的Something.prototype
?
在GeneratorFunction Objects段中,有一张关系图:
问题 1
是的。
问题 2
d
是一个 IteratorPrototype。根据文档:
The following expression is one way that ECMAScript code can access
the %IteratorPrototype% object:
Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))
似乎没有 Iterator
使得 Iterator.prototype
等于 d
。
是的。让我们再分解一下:
function* g() {} // a generator function
const GeneratorFunction = Object.getPrototypeOf(g).constructor;
console.assert(Object.getPrototypeOf(GeneratorFunction) == Function);
const Generator = GeneratorFunction.prototype;
console.assert(Object.getPrototypeOf(Generator) == Function.prototype);
const ArrayIteratorPrototype = Object.getPrototypeOf([].values());
const a = g(); // a generator
console.assert(a instanceof g);
const b = Object.getPrototypeOf(a); // a prototype object
console.assert(b == g.prototype);
const c = Object.getPrototypeOf(b); // the Generator.prototype
console.assert(c == Generator.prototype);
const d = Object.getPrototypeOf(c); // the Iterator.prototype
console.assert(d == Object.getPrototypeOf(ArrayIteratorPrototype));
const e = Object.getPrototypeOf(d); // the Object.prototype
console.assert(e == Object.prototype);
const f = Object.getPrototypeOf(e); // null
console.assert(f == null);
您要查找的对象 d
是所有迭代器(数组迭代器、映射迭代器、集合迭代器、字符串迭代器等)共享的原型对象,包括生成器。没有全局 Iterator
(yet) 就像没有全局 Generator
或 GeneratorFunction
.
我正在研究 JavaScript 生成器,根据以下代码及其输出有 2 个问题:
const a = function*(){}(); // Object [Generator] {}
const b = a.__proto__; // Object [Generator] {}
const c = b.__proto__; // Object [Generator] {}
const d = c.__proto__; // {}
const e = d.__proto__; // {}
const f = e.__proto__; // null
console.log(a, b, c, d, e, f);
问题 1
似乎每个生成器对象都有自己独特的原型,而且它们都有一个共同的原型:
const x = function*(){}();
const y = x.__proto__;
const z = y.__proto__;
console.log(b === y); // false
console.log(c === z); // true
我上面的理解对吗?
问题 2
因为f
是null
,e
可能是Object.prototype
:
console.log(e === Object.prototype); // true
但是,我想不通d
是什么。有没有等于d
的Something.prototype
?
在GeneratorFunction Objects段中,有一张关系图:
问题 1
是的。
问题 2
d
是一个 IteratorPrototype。根据文档:
The following expression is one way that ECMAScript code can access the %IteratorPrototype% object:
Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))
似乎没有 Iterator
使得 Iterator.prototype
等于 d
。
是的。让我们再分解一下:
function* g() {} // a generator function
const GeneratorFunction = Object.getPrototypeOf(g).constructor;
console.assert(Object.getPrototypeOf(GeneratorFunction) == Function);
const Generator = GeneratorFunction.prototype;
console.assert(Object.getPrototypeOf(Generator) == Function.prototype);
const ArrayIteratorPrototype = Object.getPrototypeOf([].values());
const a = g(); // a generator
console.assert(a instanceof g);
const b = Object.getPrototypeOf(a); // a prototype object
console.assert(b == g.prototype);
const c = Object.getPrototypeOf(b); // the Generator.prototype
console.assert(c == Generator.prototype);
const d = Object.getPrototypeOf(c); // the Iterator.prototype
console.assert(d == Object.getPrototypeOf(ArrayIteratorPrototype));
const e = Object.getPrototypeOf(d); // the Object.prototype
console.assert(e == Object.prototype);
const f = Object.getPrototypeOf(e); // null
console.assert(f == null);
您要查找的对象 d
是所有迭代器(数组迭代器、映射迭代器、集合迭代器、字符串迭代器等)共享的原型对象,包括生成器。没有全局 Iterator
(yet) 就像没有全局 Generator
或 GeneratorFunction
.