Is there a name for partial function application with a fixed argument list?
I'm trying to find out if there's a concept or an existing开发者_StackOverflow中文版 formal name for this use of partial function application/binding.
I've seen it in multiple places/libraries in different names but here's more or less the concepts I'm talking about vs. the normal curry/bind/partial function application:
//Typical usage of bind/curry:
fn = bind(function(){console.log(this, arguments)}, context, 'arg 1', 'arg 2');
fn('arg 3'); //logs out arg 1, arg 2, and arg 3
//What I'm looking for:
fn = makeFn(function(){console.log(this, arguments)}, context, 'arg 1', 'arg 2');
fn('arg 3'); //logs out arg 1, arg 2
// (Notice that arg 3 is ignored because it's not in the defined list of arguments).
An example use case for something like this:
obj = {
toggle: function(force, config){
node[force || this.hidden ? 'show' : 'hide'](config && config.someSetting);
this.hidden = !this.hidden;
},
hidden: false
}
node.on('click', makeFn(obj.toggle, obj, true));
(using bind/curry pass along an event object or whatever other arguments are sent in, but the function we're defining would be trying to optionally use that as a completely different type of argument).
I've seen this pattern called multiple things in different libraries with some slightly different syntaxes/abilities.
Facebook's js uses Function.prototype.shield(context, [args...]), Mootools uses Function.prototype.pass(args, [context]), Sencha (née ExtJS) has Function.prototype.createDelegate(fn, args, [appendArgs = false]) and I've even seen some called partial(fn, context, args) (however partial usually has the added ability to pass around a placeholder to allow the passing of different default arguments; I'm avoiding this due to the performance overhead of looping over the argument list on every function call).
Is this a formal concept, or is it only an implied one that requires some new name?
Thanks in advance :)
精彩评论