0.0.3 • Published 5 months ago

@yourock88/bind-self v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Automatic bind all class methods to instance

install

npm i @yourock88/bind-self

import

import bindSelf from '@yourock88/bind-self'

or

const bindSelf = require('@yourock88/bind-self').default // only for nodejs >= v22

example

class A {
  constructor() {
    bindSelf(this) // bind all class A methods to instance that was created by new expression
    this.n = 42
  }
  getN() {
    return this.n
  }
}

class B extends A {
  constructor() {
    super()
    console.log('>', this.n)
  }
  getN() {
    return super.getN()
  }
}

function fn(cb) {
  const result = cb()
  console.log('>>', result)
}

const a = new A()
const b = new B() // > 42

fn(a.getN) // >> 42
fn(b.getN) // >> 42
b.n = 101
fn(b.getN) // >> 101
0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago