File this one under “just curious” or “is it possible?”
Say for example I have…
home: function(options) {
this._home('home', options)
}
login: function(options) {
this._home('login', options)
}
home and login are obviously identifiers on dozens of object properties for tracking purposes. Is there a way to just return home or login without using any variables (an external function call is fine) within the object property?
UPDATE: Turns out this isn’t possible. The accepted answer doesn’t exactly answer the question, but it is a wonderful example of simplifying numerous calls to the same property.
If you mean that within the function that the
homeproperty references you want to be able to somehow get the string"home"from that property name without hardcoding it then no, to the best of my knowledge that isn’t possible.Just guessing at what you’re trying to achieve, would something like this help at least a little bit:
At least then you don’t have to repeat the same function body for each property. Demo: http://jsfiddle.net/EeEAw/
Note: I assume that the
_home()function invoked but not defined in the question would be defined somewhere in the real-world code. I don’t show it in my answer, though I created a dummy one in my fiddle.Just as an aside, note that the function doesn’t really “belong” to the object or to the property – there’s nothing stopping you doing this sort of thing:
That is, you can create multiple references to the same function, and the function will continue to exist even if the original
obj.homeproperty is set to some other value (as long as the additional references continue to exist).