For example if you are creating a widget that can take a url parameter it would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This is a pretty common behaviour and many Backbone properties can be configured this way.
Now, to retreive the value you can check if it’s a function or a string and handle it manually but this is such a common thing there’s an underscore method for that.
Inside the widget code whenever we need the url value we can call
to return it:
1 2 3
This means we don’t have to check ourselves if the url was a function or a string - we let underscore do that.
This little utility method is awesome and Backbone uses it all over the place (a quick search of the codebase shows that it’s used 12 times). It’s also a great way to make your client-side framework APIs flexible.
_.result() is now my new favourite underscore method.