Underscore.js
 

Underscore.js

on

  • 992 views

 

Statistics

Views

Total Views
992
Views on SlideShare
983
Embed Views
9

Actions

Likes
0
Downloads
20
Comments
0

5 Embeds 9

http://sstestblogthing.blogspot.com 4
http://staging.slideshare.com 2
http://pinterest.com 1
http://0.0.0.0 1
http://sstestblogthing.wordpress.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Underscore.js Underscore.js Presentation Transcript

  • Underscore.js an introduction
  • Collections in JS are a pain!
  • What do I get?_.functions(_) => [ after, all, any, bind, bindAll, clone,compact, compose, contains, debounce, defaults,defer, delay, difference, each, escape, extend, filter,find, first, flatten, functions, groupBy, head, identity,include, indexOf, initial, intersect, intersection, invoke,isArguments, isArray, isBoolean, isDate, isElement,isEmpty, isEqual, isFunction, isNaN, isNull, isNumber,isObject, isRegExp, isString, isUndefined, keys, last,lastIndexOf, map, max, memoize, methods, min,mixin, noConflict, once, pluck, range, reduce,reduceRight, reject, rest, select, shuffle, size, some,sortBy, sortedIndex, tail, tap, template, throttle, times,toArray, union, uniq, unique, uniqueId, values,without, wrap, zip ] View slide
  • Functional vs Object-OrientedObject-Oriented style:_(arr).pluck(name)Functional style:_.pluck(arr, name) View slide
  • Sorting an array of objectsvar ppl = [ { name: Alice, age: 20 }, { name:Bob, age: 18 }, { name: Charles, age: 23 } ]_.sortBy(ppl, function(x){ return -1 * x.age })=> [{"name":"Charles","age":23}, {"name":"Alice","age":20}, {"name":"Bob","age":18}]
  • Chaining multiple functionsvar ppl = [ { name: Alice, age: 20 }, { name:Bob, age: 18 }, { name: Charles, age: 23 }, {name: Roberto, age:21 }]_(ppl).chain().reject(function(x) { return /ob/i.test(x.name) }).sortBy(function(x) { return -1 * x.age }).pluck(name).value() => ["Charles", "Alice" ]
  • Using "range" and "reduce" to makea factorial function_.range(1,11) = [1,2,3,4,5,6,7,8,9,10]var fac = function(x) { return _.reduce(_.range(1,x+1), function(total, y) { return total * y })}
  • Array/object equality[hello] == [hello]=> false{foo:bar} == {foo:bar}=> false_.isEqual([hello], [hello])=> true_.isEqual({foo:bar}, {foo:bar})
  • "Inheritance", _-stylevar foo = { one: 1, two: 2 }var bar = { three: 3 }_.extend(foo,bar)=> {"one":1,"two":2,"three":3}
  • Throttlingvar throttled = _.throttle(updatePosition, 100);$(window).scroll(throttled);
  • Places you can use it
  • Thanks!Sebastian Motraghi@seb_m http://blog.motraghi.us@xoxco http://xoxco.com