下划线的学习2
对于上次的内容有个地方值得补充一下:
_[first ? 'find' : 'filter'](obj, function(value) {...
就是这样的语法,原来函数还能这么调用,用这样的结构,写出来的代码岂不是可以动态递归自身了么?
从这里开始加速度
invoke_.invoke(list, methodName, [*arguments])
Calls the method named by methodName on each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); => [[1, 5, 7], [1, 2, 3]]
这就是一反射功能,同时还结合了函数式的集合.通过方法名,参数来调用.并作用于list.目测这功能非常强大.非常动态.这岂不是意味着可以轻易在在html的大堆标记里,弄上一套DSL语言么.
pluck_.pluck(list, propertyName)
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.pluck(stooges, 'name'); => ["moe", "larry", "curly"]
这就是一投影功能,相当于Sql的select,linq的匿名对象.不过这个pluck名字有点生,要我说, 不如用select,其实可以考虑定义一个select别名.
max_.max(list, [iterator], [context])
Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the value is ranked.
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.max(stooges, function(stooge){ return stooge.age; }); => {name : 'curly', age : 60};
min_.min(list, [iterator], [context])
Returns the minimum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the value is ranked.
var numbers = [10, 5, 100, 2, 1000]; _.min(numbers); => 2
sortBy_.sortBy(list, iterator, [context])
Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator. Iterator may also be the string name of the property to sort by (eg. length).
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); }); => [5, 4, 6, 3, 1, 2]
groupBy_.groupBy(list, iterator, [context])
Splits a collection into sets, grouped by the result of running each value throughiterator. If iterator is a string instead of a function, groups by the property named byiterator on each of the values.
_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); }); => {1: [1.3], 2: [2.1, 2.4]} _.groupBy(['one', 'two', 'three'], 'length'); => {3: ["one", "two"], 5: ["three"]}
很强大的分组功能.
countBy_.countBy(list, iterator, [context])
Sorts a list into groups and returns a count for the number of objects in each group. Similar to groupBy, but instead of returning a list of values, returns a count for the number of values in that group.
_.countBy([1, 2, 3, 4, 5], function(num) { return num % 2 == 0 ? 'even' : 'odd'; }); => {odd: 3, even: 2}
shuffle_.shuffle(list)
Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle.
_.shuffle([1, 2, 3, 4, 5, 6]); => [4, 1, 6, 3, 5, 2]
混乱:把list打乱
toArray_.toArray(list)
Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object.
(function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4); => [2, 3, 4]
和大多数做法一样,通常用来处理参数
size_.size(list)
Return the number of values in the list.
_.size({one : 1, two : 2, three : 3}); => 3