标签:特殊 date() contex key null one 说明 回调 nts
_.each(list, iteratee, [context])
依次对集合的所有元素进行某种操作,原样返回list。接收3个参数,list集合可以理解为数据源;iteratee即迭代器可以理解为回调方法;context执行上下文。
//遍历数组 _.each([1, 2, 3], function (element, index, list) { console.log(element); }); //遍历对象 _.each({one: ‘一‘, two: ‘二‘, three: ‘三‘}, function(value, key, list){ console.log(value); }); //遍历字符串 _.each(‘123‘, function(element, index, list){ console.log(element); }); //遍历arguments function abc(){ _.each(arguments, function(element, index, list){ console.log(element); }); } abc(1, 2, 3);
//数组的情况 _.each([1, 2, 3], function (element, index, list) { console.log(element, index, list); //1 0 [1, 2, 3] //2 1 [1, 2, 3] //3 2 [1, 2, 3] }); //对象的情况 _.each({one: ‘一‘, two: ‘二‘, three: ‘三‘}, function(value, key, list){ console.log(value, key, list); //一 one Object {one: "一", two: "二", three: "三"} //二 two Object {one: "一", two: "二", three: "三"} //三 three Object {one: "一", two: "二", three: "三"} });
_.each([1], function (element, index, list) { console.log(this); //window }); _.each([1], function (element, index, list) { console.log(this); //{key: 1} }, {key : 1});
var arr1 = _.each([1, 2, 3], function (element, index, list) { }); var arr2 = _.each({one: ‘一‘, two: ‘二‘, three: ‘三‘}, function(element, index, list){ }); console.log(arr1); //[1, 2, 3] console.log(arr2); //Object {one: "一", two: "二", three: "三"}
_.forEach([1, 2, 3], function (element, index, list) { console.log(element); });
_.each(null, function (element, index, list) { console.log(element); //不执行 }); _.each(undefined, function (element, index, list) { console.log(element); //不执行 }); _.each(123, function (element, index, list) { console.log(element); //不执行 }); _.each(new Date(), function (element, index, list) { console.log(element); //不执行 });
_.each([1, 2, 3], alert); //会弹三次
underscorejs类库之_.each(list, iteratee, [context])
标签:特殊 date() contex key null one 说明 回调 nts
原文地址:http://www.cnblogs.com/rechel/p/7615040.html