-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bind factory: create partial function applications with injected arguments #167
base: master
Are you sure you want to change the base?
Conversation
Thanks @protomouse! This looks pretty good overall. I'll take a closer look at the code shorty. |
Updated PR after feedback from @briancavalier. |
lgtm. Any thoughts, @unscriptable? |
I think this a really great feature. Just a couple of notes:
|
The nicest feature of |
Good point. Should this new factory also provide the ability to bind context? I always feel like these two actions, binding context and partial application, are actually two separate things, but many JS devs will tend to think of them as belonging together since that's how
Ah yes, Another concern is that
Hmmm, I thought binding
The only 2 ways I know to do this are: 1) use |
I just ran a very simple test in Chrome to make sure. In my test, |
I tried the same in Node 0.11.14 and got the same behaviors as you did for Chrome in both strict and non-strict. |
I thought about this myself, but couldn't come up with concrete uses for this functionality. The factory consumes "free" functions, not object/prototype members. To my knowledge, there isn't (yet) a way to reference the latter in a wire spec, i.e. myObj: {
create: 'myModule'
},
myOtherObj: {
create: 'myOtherModule'
},
myFn: {
bind: {
fn: { $ref: 'myObj.myMember' },
thisArg: { $ref: 'myOtherObj' }
}
} In such a scenario, having control over
http://jsperf.com/partial-vs-partial-w-arity-vs-bind-construct (creating the functions) The |
Heh. Here's an awesome one: It's ugly, but helps performance a ton in critical parts of the code. :) |
@protomouse Thanks for doing some research. Perhaps the answer here is just to name this factory (Just a note: I'm not sure your "call" jsperf is valid since the result of the calling the functions is never used. In those cases, engines may actually optimize the function call out of existence, esp. in this case since the |
This PR implements the
bind
factory, which takes a module that is a function/constructor and a list of arguments, and creates a component that is a partial application of that function on those arguments.Use case
I'm using a third-party data format parser (
MapParser
) that supports binding constructors to annotated entities in the data. Whenever an bound entity is encountered, the parser instantiates an object by passing entity data to the corresponding constructor.Here,
entityTypes
refers to an object injected by wire:MapParser expects a unary constructor
function(entity)
, but the constructor requires additional components that must be wired in. Continuing the example above,entities/opponent.js
:In such scenarios, the
bind
factory can be useful.