-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow _.result
to be passed arguments
#886
Conversation
The reason why we didn't allow result to be passed arguments is because it breaks the parity.
If |
Possibly for things like Backbone options or cases where one property for objA is more complex than that prop for objB? peeps = [
name: 'Cher'
,
name: (full) -> "Arnold#{if full then ' Schwarzenegger' else ''}"
]
names = _.map peeps, (peep) -> _.result peep, 'name'
fullNames = _.map peeps, (peep) -> _.result peep, 'name', true I think it could be useful. Also, this is also where that |
Note that if this get's merged in I'd like to see the signature be:
Totally self serving but I think if this level of granularity is added it may as well act as an |
I think that var x = { basis: 42 };
x.prop = function (param) { return this.basis * param; }; Use binding: x.prop = x.prop.bind(x, -1);
_.result(x, 'basis');
// 42
_.result(x, 'prop');
// -42 |
Yep -- closing for the reason explained above. We don't want to allow you to pass arguments to a value that's not a function, and have them be ignored. |
Just to make it clear (it's not stated in the documentation, but I can see it in the source) The context of the function will be set to this, right? Cause I thought it might be useful to pass the object itself as a parameter, but I see you can access it with the this keyword. (BTW, this should be added to the documentation) Anyway, if we would pass the object as a parameter, we could bind the function to some other context instead... |
@opensas yep: https://github.com/documentcloud/underscore/blob/1.4.4/underscore.js#L1063 The function is |
Thanks a lot, I've just sent a pull request to update the documentation. #1007 |
true, but that should be fine. The use case for which this would be necessary is to be able to proxy options to 'url(...)' via 'fetch(...)'. More discussion of that in this issue: jashkenas/backbone#3108 |
I also changed the test file to include the uncompressed underscore, seemed odd that it was using the minified version...