Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

caseywebdev
Copy link
Contributor

I also changed the test file to include the uncompressed underscore, seemed odd that it was using the minified version...

@jashkenas
Copy link
Owner

The reason why we didn't allow result to be passed arguments is because it breaks the parity.

_.result(object, 'key', 1, 2, function(callback){ ... });

If object.key happens to be a value instead of a function, then the arguments are completely ignored. I can't think of many situations in which the property may be a value or a function (the only use case in which _.result is useful) and you'd also want to pass arguments.

@caseywebdev
Copy link
Contributor Author

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 _.f function would have been handy as _.invoke only does functions, and _.pluck only does values, but _.f would do whichever is appropriate (e.g. _.map peeps, _.f 'name', true).

@eastridge
Copy link

Note that if this get's merged in I'd like to see the signature be:

_.result(obj, key [,scope] [,...])

Totally self serving but I think if this level of granularity is added it may as well act as an apply alias on top of the existing functionality.

@StreetStrider
Copy link

I think that _.result method must do only one thing: check property, and invoke if it a function. Don't break Single responsibility principle.
Consider some object on which we wanna use _.result. We can use Function.bind to set context for functions and also set parameters if we need. Result of binding will be the function that have signature with no parameters and it can be simply invoked with _.result.

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

@jashkenas
Copy link
Owner

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.

@jashkenas jashkenas closed this Jan 30, 2013
@opensas
Copy link
Contributor

opensas commented Mar 8, 2013

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...

@akre54
Copy link
Collaborator

akre54 commented Mar 8, 2013

@opensas yep: https://github.com/documentcloud/underscore/blob/1.4.4/underscore.js#L1063

The function is called with the object argument passed as the context. You should be able to do exactly as you suggested.

opensas added a commit to opensas/underscore that referenced this pull request Mar 8, 2013
@opensas
Copy link
Contributor

opensas commented Mar 8, 2013

Thanks a lot, I've just sent a pull request to update the documentation. #1007

@maratbn
Copy link

maratbn commented Apr 7, 2014

If object.key happens to be a value instead of a function, then the arguments are completely ignored...

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants