Opened 11 years ago
Closed 10 years ago
#12107 closed feature (fixed)
Change proxy to allow arguments currying without overwriting context
Reported by: | Owned by: | ||
---|---|---|---|
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
Change History (6)
comment:1 Changed 11 years ago by
Owner: | set to [email protected]… |
---|---|
Status: | new → pending |
comment:2 Changed 11 years ago by
Status: | pending → 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 11 years ago by
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 11 years ago by
Component: | unfiled → core |
---|---|
Milestone: | None → 1.9 |
Priority: | undecided → low |
Status: | new → open |
comment:5 Changed 10 years ago by
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 10 years ago by
Resolution: | → fixed |
---|---|
Status: | open → closed |
Fix #12107. Let .proxy() curry args without overwriting context. Close gh-866.
Changeset: de9ff7cd171ebb47954ad95d50d2e41a49a7bfd2
I'm interested in this, but I want to see some use cases and test cases.