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 = restArgs(function(obj, path, args) {
var contextPath, func;
//確定function,不明白判斷數(shù)組的意義谣旁,應(yīng)該與上下文判斷有關(guān)
if (_.isFunction(path)) {
func = path;
}
// 源碼中對這里的提交commit message是Make _.invoke support deep method paths,仍不理解
else if (_.isArray(path)) {
//這里是執(zhí)行對數(shù)組的淺復(fù)制
contextPath = path.slice(0, -1);
path = path[path.length - 1];
}
//在對象上遍歷給定的方法
return _.map(obj, function(context) {
var method = func;
if (!method) {
if (contextPath && contextPath.length) {
context = deepGet(context, contextPath);
}
//在這里判斷函數(shù)執(zhí)行對象與拿到最終要執(zhí)行的方法
if (context == null) return void 0;
//方法在不同類型的原型鏈上法挨,通過key值獲取
method = context[path];
}
return method == null ? method : method.apply(context, args);
});
});
restArgs
//與ES6里的rest param相似,將多的參數(shù)整理為數(shù)組
var restArgs = function(func, startIndex) {
//一個函數(shù)的length屬性是這個函數(shù)在聲明時聲明的形參個數(shù)
//從第幾個參數(shù)開始把func的剩余參數(shù)"改造"成Rest Parameters,如果不傳第二個參數(shù)startIndex琳省,默認從最后一個參數(shù)開始
startIndex = startIndex == null ? func.length - 1 : +startIndex;
//這個方法是我們調(diào)用invoke時執(zhí)行的函數(shù)
return function() {
//創(chuàng)建一個數(shù)組涯竟,長度為包裹剩余參數(shù)的個數(shù)
//這里的arguments.length是實際參數(shù)的長度,startIndex使用形式參數(shù)算出來的袖订,兩個相減慰毅,才是真正所說的數(shù)組的長度
var length = Math.max(arguments.length - startIndex, 0),
rest = Array(length),
index = 0;
//循環(huán)賦值給創(chuàng)建的數(shù)組
for (; index < length; index++) {
rest[index] = arguments[index + startIndex];
}
//下面進行條件判斷隘截,進行核心方法的調(diào)用
//這里的func是指上面invoke方法傳給restArgs方法的匿名函數(shù)
switch (startIndex) {
case 0: return func.call(this, rest);
case 1: return func.call(this, arguments[0], rest);
case 2: return func.call(this, arguments[0], arguments[1], rest);
}
//如果startIndex過大,即除了restParameters之外有很多的參數(shù),就將他們整理成一個數(shù)組汹胃,使用apply調(diào)用,因為apply接收一個數(shù)組作為參數(shù)
var args = Array(startIndex + 1);
for (index = 0; index < startIndex; index++) {
args[index] = arguments[index];
}
args[startIndex] = rest;
return func.apply(this, args);
};
};
pluck
_.pluck(list, propertyName)
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]
_.pluck = function(obj, key) {
return _.map(obj, _.property(key));
};
遍歷對象婶芭,返回方法篩選出內(nèi)容(主要是對于map函數(shù)的理解,返回一個由遍歷結(jié)果構(gòu)成的新數(shù)組)