introduction to underscore.js

15
Introduction to Underscore.js David Jacobs @MetaThis

Upload: david-jacobs

Post on 08-May-2015

2.531 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Introduction to Underscore.js

Introduction toUnderscore.js

David Jacobs@MetaThis

Page 2: Introduction to Underscore.js

What Is Underscore?• Utility library• Provides support for functional programming

Page 3: Introduction to Underscore.js

General Purpose

Client-side and Server-side• It has nothing to do with DOM manipulation

(unlike jQuery) • No functionality specific to web browsers

Page 4: Introduction to Underscore.js

Node.js

Most depended on module in NPM http://search.npmjs.org

Page 5: Introduction to Underscore.js

Over 70 FunctionsCollections - each- map- reduce- reduceRight- find- filter- reject- all- any- include- invoke- pluck- max- min- sortBy- groupBy- sortedIndex- shuffle- toArray- size

Arrays- first- initial- last- rest- compact- flatten- without- union- intersection- difference- uniq- zip- indexOf- lastIndexOf- range

Functions- bind- bindAll- memoize- delay- defer- throttle- debounce- once- after- wrap- compose

Objects- keys- values- functions- extend- defaults- clone- tap- has- isEqual- isEmpty- isElement- isArray- isArguments- isFunction- isString- is…(more like this)

Utility- noConflict- identity- times- mixin- uniqueId- escape- template

Chaining- chain- value

Page 6: Introduction to Underscore.js

JavaScript Functions

• Functions are objects• Functions are values– Can be assigned to a variable– Can be passed to another function as an argument– Can be returned by a function

Page 7: Introduction to Underscore.js

Functional Programming

• Functional programming is using functions as a mapping from one value to another value

• Thinking and programming in a functional style means using higher-order functions– Composition– Chaining

Page 8: Introduction to Underscore.js

Map

“Transform this array into a new array, using this transformation”

Page 9: Introduction to Underscore.js

Reduce

“Accumulate a single value from an array of values”

Page 10: Introduction to Underscore.js

Filter

“Give me a subset of my array that meets my conditions”

Page 11: Introduction to Underscore.js

Memoize

“Cache the result”

Page 12: Introduction to Underscore.js

Metaprogrammingwrap _.wrap(function, wrapper) • Wraps the first function inside of the wrapper function, passing it as the

first argument. This allows the wrapper to execute code before and after the function runs, adjust the arguments, and execute it conditionally.

tap _.tap(object, interceptor) • Invokes interceptor with the object, and then returns object.

Page 13: Introduction to Underscore.js

Functional Helperscompose _.compose(*functions) • Returns the composition of a list of functions, where each function

consumes the return value of the function that follows. In math terms, composing the functions f(), g(), and h() produces f(g(h())).

Page 14: Introduction to Underscore.js

Flow Helpersafter _.after(count, function)• Creates a version of the function that will only be run after first being

called count times. Useful for grouping asynchronous responses, where you want to be sure that all the async calls have finished, before proceeding.

Page 15: Introduction to Underscore.js

More InfoWeb Sitehttp://documentcloud.github.com/underscore

Githubhttps://github.com/documentcloud/underscore/

Annotated Source Code (useful!)http://documentcloud.github.com/underscore/docs/underscore.html