In the previous chapter, we excluded some important functions that are essential in implementing the method-chaining concept. Underscore has built-in support for this concept through the _.chain(obj)
function, which returns a new instance of the _
object with the obj
parameter stored as a property of the returned object; this property is called _wrapped
. The new _
instance is different from the global _
object exclusively used up until now in all our examples. The new _
instance becomes the vehicle of the method chain started with _.chain(obj)
and we can use it to call any Underscore function without specifying the first argument. The first argument is always implied to be the _wrapped
instance, so we can write code like this:
var people = [{ name: "Herta Muller", birthYear: 1953, awardYear: 2009 }, { name: "Mario Vargas Llosa", birthYear: 1936, awardYear: 2010 }, { name: "Tomas Transtromer", birthYear: 1931, awardYear: 2011 }, ...