// http://habrahabr.ru/post/199456/
/* As it could be seen this snippet does not make code shorter and gives new undocumented function binder() that replaces standard bind(), that makes code more difficult to read cause programmer needs to keep in mind new function.
Could you provide a real need in this snipped that I probably have missed? */
var context = { a: 'a_val' };
function logArgs(x, y) {
console.log("Context a: " + this.a);
console.log(x + " : " + y);
};
var binder = Function.prototype.call.bind(Function.prototype.bind);
var logArgsBinded = binder(logArgs, context);
logArgsBinded("x_val", "y_val");
// do use:
console.log("Common way binding");
var logArgsBindedClassicaly = logArgs.bind(context);
logArgsBindedClassicaly("x_val", "y_val");
// no need in this cause does not make code simplier
var caller = Function.prototype.call.bind(Function.prototype.call);
caller(logArgs, context, "x_val", "y_val");
// do use: logArgs.call(context, "x_val", "y_val");
var applier = Function.prototype.call.bind(Function.prototype.apply);
applier(logArgs, context, ["x_val", "y_val"]);
// do use: logArgs.apply(context, ["x_val", "y_val"]);
/*
var binder = Function.prototype.call.bind(Function.prototype.bind);
turns: var fBound = f.bind(context);
into: var fBound = binder(f,context);
var caller = Function.prototype.call.bind(Function.prototype.call);
turns: f.call(x,y);
into: caller(f,context,x,y);
var applier = Function.prototype.call.bind(Function.prototype.apply);
turns: f.call(x,y);
into: caller(f,context,[x,y]);
How do I create caller and applier functions in javascript?
I've read an article [https://variadic.me/posts/2013-10-22-bind-call-and-apply-in-javascript.html] about JS snippet turning x.y(z) into y(x,z):
var binder = Function.prototype.call.bind(Function.prototype.bind);
My idea was to create caller and applier functions that follow this example
So there are actually two questions:
1. Why do my caller and applier execute the way they...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.