Bug Tracker

Opened 5 years ago

Closed 5 years ago

#12107 closed feature (fixed)

Change proxy to allow arguments currying without overwriting context

Reported by: bugs.jquery@… Owned by: bugs.jquery@…
Priority: low Milestone: 1.9
Component: core Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:


jQuery currently does not have a curry function. Proxy does support arguments currying but will always overwrite the context. Sometimes I would like to only curry some arguments without setting an explicit context.

I'm not sure if this already has been suggested, as the change is very small and quite obvious. This enables me to use it to only curry arguments without an explicit context:

var cb = jQuery.proxy(fn, null, 'arg1', 'arg2');
cb.call(myobject, 'arg3');

I already opened a pull request on github: https://github.com/jquery/jquery/pull/866


  proxy = function() {
-   return fn.apply( context, args.concat( core_slice.call( arguments ) ) );
+   return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) );

Scott Gonzalez already made a valid point: "I wonder if there are people intentionally passing null to get the methods to run in the global context. I doubt it, but it's something to be aware of if this lands."

Sorry if the pull request was to soon, hope you don't mind. I agree that this change could brake code in some edge cases. I think there are two ways this could go. Take the shortest code possible or minimize the impact on existing code.

My current patch would be the golf version. It would change the behavior of 0, null, false and undefined (maybe there are more?). I also checked "" and "0". They evaluate to a string and take another code path. A stricter version could check for 'context === null'. Maybe it would make sense to enable the stricter version in a minor release and the shorter version in a major release?

What do you think? Is it worth adding to jQuery? I personally would find this feature quite handy from time to time!

Best wishes Marcel Greter

Change History (6)

comment:1 Changed 5 years ago by Rick Waldron

Owner: set to bugs.jquery@…
Status: newpending

I'm interested in this, but I want to see some use cases and test cases.

comment:2 Changed 5 years ago by bugs.jquery@…

Status: pendingnew

I created a very short use case here: http://jsfiddle.net/MJwB7/31/

It basically does the same as prototype's curry function: http://prototypejs.org/api/function/curry

comment:3 Changed 5 years ago by anonymous

Just for the record, it Is possible to use primitives (including null) as the this value in strict mode. Not sure if jQuery supports strict mode (I hope so), but this might be good to know.

comment:4 Changed 5 years ago by dmethvin

Component: unfiledcore
Milestone: None1.9
Priority: undecidedlow
Status: newopen

comment:5 Changed 5 years ago by dmethvin

I agree that it would be theoretically possible to use falsy values there but it would make no sense. They would all be promoted to wrapped objects so the this wouldn't be falsy when it got to the function. Imma go ahead and land this.

comment:6 Changed 5 years ago by Marcel Greter

Resolution: fixed
Status: openclosed

Fix #12107. Let .proxy() curry args without overwriting context. Close gh-866.

Changeset: de9ff7cd171ebb47954ad95d50d2e41a49a7bfd2

Note: See TracTickets for help on using tickets.