jashkenas/underscore — pull request #1241 by jridgewell (2013-08-01 05:18:08+00:00)
Title: _.attempt(object, 'method', [*arguments])

A helper function, similar to Rails' `try`, that will call the method on object, unless object is undefined or null. Passes the 3rd+ argument as the arguments to the method.

Often when cleaning up Backbone views, I need to remove a subview that may have been attached:

```javascript
if (this.subView && this.subView.close) { this.subView.close(); }
```

`_.attempt` will do the same, turning that into:
```javascript
_.attempt(this.subView, 'close');
```

This is extremely similar to `_.result`, with the distinction that you use `attempt` to call a method on an object that may or may not be defined and you can pass arguments, not just evaluating a (potentially function) property.