es6 bind

by Valentin Sarychev

JavaScript

Function.prototype.bind2 = function(context, ...args) {
	return (...rest) => this.apply(context, [...args, ...rest]);
};

// === test ===

const testObject = (...args) => args.join('');
testObject.test = function(...args) {
	return this(...args);
};

const test = testObject.test.bind2(testObject, 1, 2);

document.body.innerText = test(3, 4) === '1234' ? 'success' : 'fail';