Skip to content
/ yoke Public

JS utility to create and reuse functions bindings for safe unregistering from event dispatchers

Notifications You must be signed in to change notification settings

ralekna/yoke

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yoke

JS utility to create and reuse functions bindings for safe unregistering from event dispatchers

Example

function add(value) {
    this + value;
}

// traditional approach
var addTwo_1 = add.bind(2);
var addTwo_2 = add.bind(2);

console.log(addTwo_1(3) + addTwo_2(1)); // 8
console.log(addTwo_1 === addTwo_2); // false - native function binding always creates new functions everytime you bind

// yoke approach
var yoke = require('yoke');

var addTwo_1 = yoke(add, 2);
var addTwo_2 = yoke(add, 2);

console.log(addTwo_1(3) + addTwo_2(1)); // 8
console.log(addTwo_1 === addTwo_2); // true - yoke saved bound function reference to its internal storage, so next time you create binding with same params it would return existing instance 

About

JS utility to create and reuse functions bindings for safe unregistering from event dispatchers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published