like-bind-operator
v0.0.4
Published
Library to provide an alternative to the This-Binding Syntax proposal compatible with ES6.
Downloads
11
Readme
Like Bind-Operator
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]();