180
180
< div id ="sidebar " class ="interface ">
181
181
182
182
< a class ="toc_title " href ="# ">
183
- Underscore.js < span class ="version "> (1.6 .0)</ span >
183
+ Underscore.js < span class ="version "> (1.7 .0)</ span >
184
184
</ a >
185
185
< ul class ="toc_section ">
186
186
< li > » < a href ="https://github.com/jashkenas/underscore "> GitHub Repository</ a > </ li >
311
311
< li > - < a href ="#times "> times</ a > </ li >
312
312
< li > - < a href ="#random "> random</ a > </ li >
313
313
< li > - < a href ="#mixin "> mixin</ a > </ li >
314
+ < li > - < a href ="#iteratee "> iteratee</ a > </ li >
314
315
< li > - < a href ="#uniqueId "> uniqueId</ a > </ li >
315
316
< li > - < a href ="#escape "> escape</ a > </ li >
316
317
< li > - < a href ="#unescape "> unescape</ a > </ li >
355
356
</ p >
356
357
357
358
< p >
358
- Underscore provides 80 -odd functions that support both the usual
359
+ Underscore provides 115 -odd functions that support both the usual
359
360
functional suspects: < b > map</ b > , < b > filter</ b > , < b > invoke</ b > —
360
361
as well as more specialized helpers: function binding, javascript
361
362
templating, deep equality testing, and so on.
@@ -391,11 +392,11 @@ <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and u
391
392
392
393
< table >
393
394
< tr >
394
- < td > < a href ="underscore.js "> Development Version (1.6 .0)</ a > </ td >
395
+ < td > < a href ="underscore.js "> Development Version (1.7 .0)</ a > </ td >
395
396
< td > < i > 44kb, Uncompressed with Plentiful Comments</ i > </ td >
396
397
</ tr >
397
398
< tr >
398
- < td > < a href ="underscore-min.js "> Production Version (1.6 .0)</ a > </ td >
399
+ < td > < a href ="underscore-min.js "> Production Version (1.7 .0)</ a > </ td >
399
400
< td >
400
401
< i > 5.0kb, Minified and Gzipped</ i >
401
402
< small > (< a href ="underscore-min.map "> Source Map</ a > )</ small >
@@ -477,15 +478,15 @@ <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
477
478
< b class ="header "> reduce</ b > < code > _.reduce(list, iteratee, [memo], [context])</ code >
478
479
< span class ="alias "> Aliases: < b > inject, foldl</ b > </ span >
479
480
< br />
480
- Also known as < b > inject</ b > and < b > foldl</ b > , reduce boils down a < b > list</ b > of values into a single value.
481
- < b > Memo</ b > is the initial state of the reduction, and each successive step of it should be returned by
482
- < b > iteratee</ b > . The iteratee is passed four arguments: the < tt > memo</ tt > , then the < tt > value</ tt > and
483
- < tt > index</ tt > (or key) of the iteration, and finally a reference to the entire < tt > list</ tt > .
481
+ Also known as < b > inject</ b > and < b > foldl</ b > , reduce boils down a < b > list</ b > of values into a single value.
482
+ < b > Memo</ b > is the initial state of the reduction, and each successive step of it should be returned by
483
+ < b > iteratee</ b > . The iteratee is passed four arguments: the < tt > memo</ tt > , then the < tt > value</ tt > and
484
+ < tt > index</ tt > (or key) of the iteration, and finally a reference to the entire < tt > list</ tt > .
484
485
</ p >
485
486
< p >
486
- If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element
487
- of the list. The first element is instead passed as the memo in the invocation of the iteratee on the next
488
- element in the list.
487
+ If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element
488
+ of the list. The first element is instead passed as the memo in the invocation of the iteratee on the next
489
+ element in the list.
489
490
</ p >
490
491
< pre >
491
492
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
@@ -1202,6 +1203,21 @@ <h2 id="functions">Function (uh, ahem) Functions</h2>
1202
1203
note.asyncSave({success: renderNotes});
1203
1204
});
1204
1205
// renderNotes is run once, after all notes have saved.
1206
+ </ pre >
1207
+
1208
+ < p id ="before ">
1209
+ < b class ="header "> before</ b > < code > _.before(count, function)</ code >
1210
+ < br />
1211
+ Creates a version of the function that can be called no more than
1212
+ < b > count</ b > times. The result of the last function call is memoized and
1213
+ returned when < b > count</ b > has been reached.
1214
+ </ p >
1215
+ < pre >
1216
+ var monthlyMeeting = _.before(3, askForRaise);
1217
+ monthlyMeeting();
1218
+ monthlyMeeting();
1219
+ monthlyMeeting();
1220
+ // the result of any subsequent calls is the same as the second call
1205
1221
</ pre >
1206
1222
1207
1223
< p id ="wrap ">
@@ -1689,6 +1705,19 @@ <h2 id="utility">Utility Functions</h2>
1689
1705
});
1690
1706
_("fabio").capitalize();
1691
1707
=> "Fabio"
1708
+ </ pre >
1709
+
1710
+ < p id ="iteratee ">
1711
+ < b class ="header "> iteratee</ b > < code > _.iteratee(value, [context], [argCount])</ code >
1712
+ < br />
1713
+ A mostly-internal function to generate callbacks that can be applied
1714
+ to each element in a collection, returning the desired result — either
1715
+ identity, an arbitrary callback, a property matcher, or a property accessor.
1716
+ </ p >
1717
+ < pre >
1718
+ var stooges = [{name: 'curly', age: 25}, {name: 'moe', age: 21}, {name: 'larry', age: 23}];
1719
+ _.map(stooges, _.iteratee('age'));
1720
+ => [25, 21, 23];
1692
1721
</ pre >
1693
1722
1694
1723
< p id ="uniqueId ">
@@ -1749,18 +1778,16 @@ <h2 id="utility">Utility Functions</h2>
1749
1778
</ pre >
1750
1779
1751
1780
< p id ="template ">
1752
- < b class ="header "> template</ b > < code > _.template(templateString, [data], [ settings])</ code >
1781
+ < b class ="header "> template</ b > < code > _.template(templateString, [settings])</ code >
1753
1782
< br />
1754
1783
Compiles JavaScript templates into functions that can be evaluated
1755
1784
for rendering. Useful for rendering complicated bits of HTML from JSON
1756
1785
data sources. Template functions can both interpolate variables, using
1757
1786
< tt > <%= … %></ tt > , as well as execute arbitrary JavaScript code, with
1758
1787
< tt > <% … %></ tt > . If you wish to interpolate a value, and have
1759
- it be HTML-escaped, use < tt > <%- … %></ tt > When you evaluate a template function, pass in a
1760
- < b > data</ b > object that has properties corresponding to the template's free
1761
- variables. If you're writing a one-off, you can pass the < b > data</ b >
1762
- object as the second parameter to < b > template</ b > in order to render
1763
- immediately instead of returning a template function. The < b > settings</ b > argument
1788
+ it be HTML-escaped, use < tt > <%- … %></ tt > When you evaluate a
1789
+ template function, pass in a < b > data</ b > object that has properties
1790
+ corresponding to the template's free variables. The < b > settings</ b > argument
1764
1791
should be a hash containing any < tt > _.templateSettings</ tt > that should be overridden.
1765
1792
</ p >
1766
1793
@@ -1769,10 +1796,6 @@ <h2 id="utility">Utility Functions</h2>
1769
1796
compiled({name: 'moe'});
1770
1797
=> "hello: moe"
1771
1798
1772
- var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
1773
- _.template(list, {people: ['moe', 'curly', 'larry']});
1774
- => "<li>moe</li><li>curly</li><li>larry</li>"
1775
-
1776
1799
var template = _.template("<b><%- value %></b>");
1777
1800
template({value: '<script>'});
1778
1801
=> "<b>&lt;script&gt;</b>"</ pre >
@@ -1998,6 +2021,63 @@ <h2 id="links">Links & Suggested Reading</h2>
1998
2021
1999
2022
< h2 id ="changelog "> Change Log</ h2 >
2000
2023
2024
+ < p id ="1.7.0 ">
2025
+ < b class ="header "> 1.7.0</ b > — < small > < i > August 26, 2014</ i > </ small > — < a href ="https://github.com/jashkenas/underscore/compare/1.6.0...1.7.0 "> Diff</ a > — < a href ="https://cdn.rawgit.com/jashkenas/underscore/1.7.0/index.html "> Docs</ a > < br />
2026
+ < ul >
2027
+ < li >
2028
+ For consistency and speed across browsers, Underscore now ignores
2029
+ native array methods for < tt > forEach</ tt > , < tt > map</ tt > , < tt > reduce</ tt > ,
2030
+ < tt > reduceRight</ tt > , < tt > filter</ tt > , < tt > every</ tt > , < tt > some</ tt > ,
2031
+ < tt > indexOf</ tt > , and < tt > lastIndexOf</ tt > . "Sparse" arrays are
2032
+ officially dead in Underscore.
2033
+ </ li >
2034
+ < li >
2035
+ Added < tt > _.iteratee</ tt > to customize the iterators used by collection
2036
+ functions. Many Underscore methods will take a string argument for easier
2037
+ < tt > _.property</ tt > -style lookups, an object for < tt > _.where</ tt > -style
2038
+ filtering, or a function as a custom callback.
2039
+ </ li >
2040
+ < li >
2041
+ Added < tt > _.before</ tt > as a counterpart to < tt > _.after</ tt > .
2042
+ </ li >
2043
+ < li >
2044
+ Added < tt > _.negate</ tt > to invert the truth value of the passed-in
2045
+ predicate.
2046
+ </ li >
2047
+ < li >
2048
+ Added < tt > _.noop</ tt > as a handy empty placeholder function.
2049
+ </ li >
2050
+ < li >
2051
+ < tt > _.isEmpty</ tt > now works with < tt > arguments</ tt > objects.
2052
+ </ li >
2053
+ < li >
2054
+ < tt > _.has</ tt > now guards against nullish objects.
2055
+ </ li >
2056
+ < li >
2057
+ Override base methods like < tt > each</ tt > and < tt > some</ tt >
2058
+ and they'll be used internally by other Underscore functions too.
2059
+ </ li >
2060
+ < li >
2061
+ The escape functions handle backticks (< tt > `</ tt > ), to deal with an
2062
+ IE ≤ 8 bug.
2063
+ </ li >
2064
+ < li >
2065
+ < tt > _.memoize</ tt > exposes the cache of memoized values as a property
2066
+ on the returned function.
2067
+ </ li >
2068
+ < li >
2069
+ < tt > _.pick</ tt > accepts `iteratee` and `context` arguments for a more
2070
+ advanced callback.
2071
+ </ li >
2072
+ < li >
2073
+ Underscore templates no longer accept an initial < tt > data</ tt > object.
2074
+ < tt > _.template</ tt > always returns a function now.
2075
+ </ li >
2076
+ < li >
2077
+ Optimizations and code cleanup aplenty.
2078
+ </ li >
2079
+ </ ul >
2080
+ </ p >
2001
2081
2002
2082
< p id ="1.6.0 ">
2003
2083
< b class ="header "> 1.6.0</ b > — < small > < i > February 10, 2014</ i > </ small > — < a href ="https://github.com/jashkenas/underscore/compare/1.5.2...1.6.0 "> Diff</ a > — < a href ="https://cdn.rawgit.com/jashkenas/underscore/1.6.0/index.html "> Docs</ a > < br />
0 commit comments