0.1.2 • Published 4 years ago
babel-plugin-generator-bind-this v0.1.2
Usage
yarn add babel-plugin-generator-bind-this -D
activate in .babelrc
{
"plugins": ["babel-plugin-generator-bind-this"]
}
Why
- generator cannot use arrow-function
- check the special case below:
in this case the generator inresult1
cannot get class propertybindValue
"class value"
, but got"scope value"
, and result2 can get"class value"
generator.bind(this)
by yourself or automatically by this plugin
const doSomething = (method) => {
return {
bindValue: 'scope value',
method
}
}
class Base {}
class Test extends Base {
bindValue = 'class value'
result1 = doSomething(function* (param) {
console.log(param, this.bindValue)
})
result2 = doSomething(
function* (param) {
console.log(param, this.bindValue)
}.bind(this)
)
}
const ins = new Test()
ins.result1.method('case1').next()
ins.result2.method('case2').next()
Demo
yarn transform
, check the output in transform/output.js