Bug Tracker

Ticket #12107 (closed feature: fixed)

Opened 2 years ago

Last modified 21 months ago

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:
Blocking: Blocked by:

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

Change History

comment:1 Changed 2 years ago by rwaldron

  • Owner set to bugs.jquery@…
  • Status changed from new to pending

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

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

  • Status changed from pending to new

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 2 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 2 years ago by dmethvin

  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to core
  • Milestone changed from None to 1.9

comment:5 Changed 21 months 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 21 months ago by Marcel Greter

  • Status changed from open to closed
  • Resolution set to fixed

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

Changeset: de9ff7cd171ebb47954ad95d50d2e41a49a7bfd2

Note: See TracTickets for help on using tickets.