Skip to main content

Bug Tracker

Side navigation

#12107 closed feature (fixed)

Opened July 19, 2012 07:55PM UTC

Closed October 21, 2012 02:11AM UTC

Change proxy to allow arguments currying without overwriting context

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

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

https://github.com/mgreter/jquery/commit/5f3f772d12c3c5be2654923ca22f144e52804d33

  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

Attachments (0)
Change History (6)

Changed July 20, 2012 02:15AM UTC by rwaldron comment:1

owner: → bugs.jquery@ocbnet.ch
status: newpending

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

Changed July 21, 2012 12:36AM UTC by bugs.jquery@ocbnet.ch comment:2

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

Changed August 10, 2012 07:24PM UTC by anonymous comment:3

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.

Changed August 21, 2012 01:53AM UTC by dmethvin comment:4

component: unfiledcore
milestone: None1.9
priority: undecidedlow
status: newopen

Changed October 21, 2012 01:55AM UTC by dmethvin comment:5

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.

Changed October 21, 2012 02:11AM UTC by Marcel Greter comment:6

resolution: → fixed
status: openclosed

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

Changeset: de9ff7cd171ebb47954ad95d50d2e41a49a7bfd2