like-bind-operator

0.0.4 • Public • Published

Like Bind-Operator

npm version Build Status

This library is meant to provide an alternative to the This-Binding Syntax compatible with ES6.

It tries to provide a short, readable syntax to bind functions. It gets even better if the environment supports Proxy.

Note: It will not work if the value to bind is undefined, null or an object without prototype (for example Object.create(null)).

Examples

// Use whatever variable name that you like for the operator
import $ from 'like-bind-operator';

function logThis() {
  console.log(this);
}

const object = {};
const array = [];
const number = 1;
const boolean = true;
const func = () => 1;

object[$](logThis)();
array[$](logThis)();
number[$](logThis)();
boolean[$](logThis)();
func[$](logThis)();


// Can also resolve properties
const objectWithProperty = {
  objectFunc() { return 1; },
};
const arrayWithElement = [ () => 1 ];

objectWithProperty[$]('objectFunc')();
arrayWithElement[$](0)();

// With Proxy
objectWithProperty[$].objectFunc();
arrayWithElement[$][0]();

Readme

Keywords

Package Sidebar

Install

npm i like-bind-operator

Weekly Downloads

4

Version

0.0.4

License

MIT

Last publish

Collaborators

  • volune