ES6学习——生成器(Generators):生成器中的this与super
2016-01-27 16:23
239 查看
先看看生成器中的this:
在规范的14.4.11有这样的描述:
If the generator was invoked using [[Call]], the this binding will have already been initialized in the normal manner. If the generator was invoked using [[Construct]], the this bind is not initialized and any references
to this within the FunctionBody will produce a ReferenceError exception.
我们来验证一下:
在试试new出来的会不会抛异常:
在看一下生成器是对象方法的情况:
接下来试试super:
功能看上去是正常的,但是在规范的14.4.1中有这样的描述:
GeneratorMethod : * PropertyName ( StrictFormalParameters ) { GeneratorBody }
It is a Syntax Error if HasDirectSuper of GeneratorMethod is true
从规范上看,生成器中并不允许调用super,但是Chrome中貌似提前实现了这些语法,这也符合Google的作风,总是领先于规范。谨慎来讲,最好先不要在生成器中使用this或者super
*以上全部代码在Chrome 48中通过测试
在规范的14.4.11有这样的描述:
If the generator was invoked using [[Call]], the this binding will have already been initialized in the normal manner. If the generator was invoked using [[Construct]], the this bind is not initialized and any references
to this within the FunctionBody will produce a ReferenceError exception.
我们来验证一下:
function* genFunc() { 'use strict'; yield this; } var [_this] = genFunc();//数组解构赋值 console.log(_this);//undefined
在试试new出来的会不会抛异常:
function* genFunc() { 'use strict'; console.log(this) } var g = new genFunc(); g.next();在Chrome下并没有抛出异常,这里和规范不一样。Chrome里this就是实例g,因为this instanceof genFunc是true。
在看一下生成器是对象方法的情况:
let obj = { *method(){yield this} }; let [methodThis] = obj.method(); console.log(methodThis === obj); // true
接下来试试super:
class A{ *method(){ yield this; } } A.prototype[Symbol.toStringTag] = "A" class B extends A{ *method(){ let [s] = super.method(); yield s; yield this; } } B.prototype[Symbol.toStringTag] = "B" var b = new B(); var [parent,child] = b.method(); console.log(parent.toString(),child.toString());//[object B] [object B] var objParent = { *method(){ yield this; }, [Symbol.toStringTag] : "objParent" } var objChild = { *method(){ var [s] = super.method(); yield s; yield this; }, [Symbol.toStringTag] : "objChild" } Object.setPrototypeOf(objChild,objParent); var [s,t] = objChild.method(); console.log(s.toString(),t.toString());//[object objChild] [object objChild]
功能看上去是正常的,但是在规范的14.4.1中有这样的描述:
GeneratorMethod : * PropertyName ( StrictFormalParameters ) { GeneratorBody }
It is a Syntax Error if HasDirectSuper of GeneratorMethod is true
从规范上看,生成器中并不允许调用super,但是Chrome中貌似提前实现了这些语法,这也符合Google的作风,总是领先于规范。谨慎来讲,最好先不要在生成器中使用this或者super
*以上全部代码在Chrome 48中通过测试
相关文章推荐
- POJ 1330 Nearest Common Ancestors
- I2C协议详解
- 报错:1130-host ... is not allowed to connect to this MySql server
- android 侧滑菜单
- 使用docker发布spring cloud应用
- dbscan聚类算法的R实现
- JAVA逻辑运算符
- linux实践-弱密码导致服务器被黑
- Cocos2d Android项目手动编译日记之SDK版本(一)
- MySQL硬件资源利用特点
- Spring-tx声明式事务、@Transaction注解事务
- 获取tableView当前选中的cell
- PAT 1050. 螺旋矩阵(25)
- 在STM32上移植FreeModbus RTU的一点经验总结
- 图像编辑之对比度调整(亮度对比度的算法公式)
- Android 自定义View (三) 圆环交替 等待效果
- java动态代理
- iOS NSNumber转化NSString之description
- paramiko模块-2
- RecyclerView初步认识