Skip to content

matthew5-library/babel-plugin-generator-bind-this

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Usage

  1. yarn add babel-plugin-generator-bind-this -D

  2. activate in .babelrc

{
  "plugins": ["babel-plugin-generator-bind-this"]
}

Why

  1. generator cannot use arrow-function
  2. check the special case below:
    in this case the generator in result1 cannot get class property bindValue "class value", but got "scope value", and result2 can get "class value"
  3. 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages