Underscore.js
Unobtrusive Utilities for Javascript




          Kevin Ball
      Co-Founder & CTO

       kball@fashioningchange.com
                 @kbal11
What is Underscore.js?
What is Underscore.js?
     Javascript Utility Library
What is Underscore.js?
     Javascript Utility Library

     • Complementary
What is Underscore.js?
     Javascript Utility Library

     • Complementary
     • Unobtrusive
What is Underscore.js?
     Javascript Utility Library

     • Complementary
     • Unobtrusive
     • Flexible
Complementary

jQuery
Complementary

jQuery
Complementary

jQuery   Functional Support
Complementary

jQuery   Functional Support

         • Collections
         • Arrays
         • Functions
Unobtrusive
Unobtrusive
      Plays nice with others
Unobtrusive
      Plays nice with others
      •Does not modify built-in
      objects
      •Delegates to built-in
      functions if present
Flexible
Flexible
      • Functional or
        Object-oriented style
      • Chainable
What Does It Do?


• Fills in holes in the language
• Provides functional support
• Makes you happier writing Javascript
Examples: Arrays


_.map([1, 2, 3], function(i) { return i * 2; });




_([1, 2, 3]).map(function(i) {return i * 2;});
Doesn’t jQuery have
      $.map?
Doesn’t jQuery have
      $.map?
_.map
Doesn’t jQuery have
      $.map?
_.map    _.each
Doesn’t jQuery have
      $.map?
_.map          _.each

    _.reduce
Doesn’t jQuery have
      $.map?
_.map          _.each   _.select

    _.reduce
Doesn’t jQuery have
      $.map?
_.map          _.each         _.select

    _.reduce       _.detect
Doesn’t jQuery have
      $.map?
_.map            _.each         _.select

      _.reduce       _.detect


_.reject
Doesn’t jQuery have
      $.map?
_.map            _.each          _.select

      _.reduce        _.detect


_.reject         _.sortBy
Doesn’t jQuery have
      $.map?
_.map            _.each          _.select

      _.reduce        _.detect


_.reject         _.sortBy         _.include
Doesn’t jQuery have
      $.map?
_.map                _.each         _.select

      _.reduce           _.detect


_.reject            _.sortBy         _.include


                 _.groupBy
Chaining

• Use with object-oriented style
• Chain method tells Underscore to return
  wrapped objects

• Value method returns the final value at the
  end
Chaining
   var lyrics   = [
  {line : 1,    words   :   "I'm a lumberjack and I'm okay"},
  {line : 2,    words   :   "I sleep all night and I work all day"},
  {line : 3,    words   :   "He's a lumberjack and he's okay"},
  {line : 4,    words   :   "He sleeps all night and he works all day"}
];

_(lyrics).chain()
  .map(function(line) { return line.words.split(' '); })
  .flatten()
  .reduce(function(counts, word) {
    counts[word] = (counts[word] || 0) + 1;
    return counts;
}, {}).value();

=> {lumberjack : 2, all : 4, night : 2 ... }



                                       * Example from http://documentcloud.github.com/underscore/
Function Utilities
Function Utilities

_.bind(function, object)
Function Utilities

_.bind(function, object)


_.memoize(function, [hashFunction])
Function Utilities

_.bind(function, object)


_.memoize(function, [hashFunction])


_.throttle(function, wait)
Object Utilities
Object Utilities

_.isNumber(object)
Object Utilities

_.isNumber(object)


_.keys(object)
Object Utilities

_.isNumber(object)


_.keys(object)


_.isEqual(object, other)
Conclusion
Conclusion




Good libraries reduce the burden
Conclusion




Good libraries reduce the burden
        Underscore.js
Thank You!

    Kevin Ball
Co-Founder & CTO

 kball@fashioningchange.com
           @kbal11

Underscore.js