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
[#2286]: Underscore could use a _.propertyResult #2531
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1370,6 +1370,21 @@ | |
|
||
_.property = property; | ||
|
||
// Generates a function for a given object that returns the passed function's return value. | ||
// Accepts a method name or function literal for value. | ||
// If value is a string, function assumes this string is a method name on the | ||
// referenced object. | ||
_.method = function(value) { | ||
return restArgs(function(obj, args) { | ||
if (obj == null) { return; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any rational why should func not be called if it is a function literal and obj is null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My current rational is that this invocation pattern doesn't make much sense to me. e.g.,
since both would be more easily written as a plain function literal, and this invocation pattern reads to me as 'call the provided method on undefined'. That said, if allowing an undefined object in this position is more in keeping with underscore's style, I'm happy to update so we can call _.method on undefined objects. |
||
var func = _.isFunction(value) ? value : obj[value]; | ||
if (func) { | ||
return func.apply(obj, args); | ||
} | ||
}); | ||
}; | ||
|
||
|
||
// Generates a function for a given object that returns a given property. | ||
_.propertyOf = function(obj) { | ||
return obj == null ? function(){} : function(key) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this not valid when a function is provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method responds to both a string literal (a method name) or a function literal. This check is to sort out if the value passed is a function literal, so that later we know what to call apply on.