bind-function-tree

0.0.4 • Public • Published

bind-function-tree

Binds functions inside object tree to the same context. Pass javascript object containing functions inside of it’s hierarchy and it will traverse them and bind to the specified context object.

It does not change the object passed to it

Usage example

import bindTree from 'bind-function-tree';
const tree = {
  handleExitClick() {
    console.log(this);
  },
  videoPlayer: {
    controls: {
      handlePlay() {
        console.log(this);
      }
    }
  }
}
const ctx = {foo: 'bar'};

bindTree(tree, ctx);

This binds all the functions(handleExitClick, handlePlay) inside tree object to the ctx. Output of both console.log calls would be {foo: 'bar'}.

Compatibility

For now it depends on Object.keys and Array.prototype.forEach to work.

Readme

Keywords

Package Sidebar

Install

npm i bind-function-tree

Weekly Downloads

1

Version

0.0.4

License

MIT

Last publish

Collaborators

  • popc0rn