-
Notifications
You must be signed in to change notification settings - Fork 84
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
Return the result of a function property #22
Comments
Well, what about the arguments of those functions? And their context? I think it's possible for you to achieve what you want if add a helper method to objectPath.method = function(obj, path, args){
var fn = objectPath.get(obj, path);
if (typeof fn === 'function') {
return fn.apply("??", args); // how would the context be properly applied in here?
}
return fn;
}; you'd call it like: objectPath.method(obj, ['get','the','func'], [1,2,3]); |
The goal is to be able to fetch data from properties we don't know the nature of. I'm proposing something very similar to lodash's The default context could be deducted by taking the A object like this: var person = {
avatar: {
url: function() { ... }
}
} Would be retrieved like this:
The context would be |
Oh I forgot to illustrate your example, it would roughly be something like this: objectPath.method = function(obj, path, args){
var fn = objectPath.get(obj, path);
var ctx = (path.length == 1 ? obj : objectPath.get(obj, path.pop() && path);
if (typeof fn === 'function') {
return fn.apply(ctx, args);
}
return fn;
}; |
The idea doesn't seem bad to me if we keep the use case simple. We would also need to find the right name and interface. For example, I think we should keep the same interface of objectPath.get(), we shouldn't pass any argument, a getter is usually without arguments, otherwise we would fall in weird situations. This is the use case I would support:
Same for the setter. What do you think @pocesar ? |
Hi,
I think it would be really nice to detect if a property is a
function
and return it's result.It could be handy when you want get a value out of a getter.
If you still want to get the function itself, it could be specified via an optional parameter to
get
.You could also create another
method
that does this, likefetch
orgetOrCall
.What do you think?
Thanks!
The text was updated successfully, but these errors were encountered: