发布于 2016-01-11 22:28:50 | 222 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的精品教程,程序狗速度看过来!

lodash JavaScript 实用工具库

lodash 是一个 JavaScript 实用工具库,提供一致性,模块化,性能和配件等功能。


Lodash 中文文档 (v3.10.1) - “Chain” 方法

Translated by PeckZeg
Original Docs: Lodash v3.10.1 Docs

“Chain” 方法

_(value)

创建一个包含 valuelodash 对象以开启内置的方法链。方法链对返回数组、集合或函数的方法产生作用,并且方法可以被链式调用。那些获取单值或可能返回一个原始值的方法将自动结束方法链并且返回一个未包裹成 lodash 对象的值。如果明确需要链式调用可以使用 _.chain。链式调用的加载将是延迟加载,这表明调用将延迟到间接或直接调用 _#value 方法。

延迟计算支持一些方法快速合并。快速合并是一个最优合并迭代器调用的策略。这样做可以帮助避免一些计算中间生成的数据结构,并且能够大大降低迭代器的执行次数。

链式调用支持自定义生成结果。只要在生成的时候直接或间接的包含 _#value 方法。

此外,对于 lodash 的方法,包装集拥有 ArrayString 的方法。

Array 包装集的方法有:

concat, join, pop, push, reverse, shift, slice, sort, spliceunshift

String 包装集的方法有:

replacesplit

支持快速合并的包装集方法有:

compact, drop, dropRight, dropRightWhile, dropWhile, filter, first, initial, last, map, pluck, reject, rest, reverse, slice, take, takeRight, takeRightWhile, takeWhile, toArraywhere

可链式调用的包装集方法有:

after, ary, assign, at, before, bind, bindAll, bindKey, callback, chain, chunk, commit, compact, concat, constant, countBy, create, curry, debounce, defaults, defaultsDeep, defer, delay, difference, drop, dropRight, dropRightWhile, dropWhile, fill, filter, flatten, flattenDeep, flow, flowRight, forEach, forEachRight, forIn, forInRight, forOwn, forOwnRight, functions, groupBy, indexBy, initial, intersection, invert, invoke, keys, keysIn, map, mapKeys, mapValues, matches, matchesProperty, memoize, merge, method, methodOf, mixin, modArgs, negate, omit, once, pairs, partial, partialRight, partition, pick, plant, pluck, property, propertyOf, pull, pullAt, push, range, rearg, reject, remove, rest, restParam, reverse, set, shuffle, slice, sort, sortBy, sortByAll, sortByOrder, splice, spread, take, takeRight, takeRightWhile, takeWhile, tap, throttle, thru, times, toArray, toPlainObject, transform, union, uniq, unshift, unzip, unzipWith, values, valuesIn, where, without, wrap, xor, zip, zipObject, zipWith

默认不能被链式调用的包装集方法有:

add, attempt, camelCase, capitalize, ceil, clone, cloneDeep, deburr, endsWith, escape, escapeRegExp, every, find, findIndex, findKey, findLast, findLastIndex, findLastKey, findWhere, first, floor, get, gt, gte, has, identity, includes, indexOf, inRange, isArguments, isArray, isBoolean, isDate, isElement, isEmpty, isEqual, isError, isFinite isFunction, isMatch, isNative, isNaN, isNull, isNumber, isObject, isPlainObject, isRegExp, isString, isUndefined, isTypedArray, join, kebabCase, last, lastIndexOf, lt, lte, max, min, noConflict, noop, now, pad, padLeft, padRight, parseInt, pop, random, reduce, reduceRight, repeat, result, round, runInContext, shift, size, snakeCase, some, sortedIndex, sortedLastIndex, startCase, startsWith, sum, template, trim, trimLeft, trimRight, trunc, unescape, uniqueId, valuewords

包装集函数 sample 在参数提供 n 的情况下将返回包装集,其他情况下将返回未经包装过的值。

参数

  1. value (*) : 待包装至 lodash 实例的值

返回

(Object) : 返回一个新的 lodash 包装集实例

示例

var wrapped = _([1, 2, 3]);

// 返回一个未包装的值
wrapped.reduce(function(total, n) {
  return total + n;
});
// → 6

// 返回一个包装值
var squares = wrapped.map(function(n) {
  return n * n;
});

_.isArray(squares);
// → false

_.isArray(squares.value());
// → true

_.chain(value)

创建一个明确能够被链式调用的 lodash 对象

参数

  1. value (*) : 待包装的值

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var users = [
  { 'user': 'barney',  'age': 36 },
  { 'user': 'fred',    'age': 40 },
  { 'user': 'pebbles', 'age': 1 }
];

var youngest = _.chain(users)
  .sortBy('age')
  .map(function(chr) {
    return chr.user + ' is ' + chr.age;
  })
  .first()
  .value();
// → 'pebbles is 1'

_.tap(value, interceptor, [thisArg])

该方法执行 interceptor 并返回 value。该拦截器将绑定 thisArg 并在执行时传入一个参数:value。该方法的目的是“利用”方法链去操作链中的中间结果。

参数

  1. value (*) : 提供给拦截器的值

  2. interceptor (Function) : 待执行的函数

  3. [thisArg] (*) : interceptor 绑定的 this

返回

(*) : 返回值

示例

_([1, 2, 3])
 .tap(function(array) {
   array.pop();
 })
 .reverse()
 .value();
// → [2, 1]

_.thru(value, interceptor, [thisArg])

This method is like _.tap except that it returns the result of interceptor.

该方法类似 _.tap,但其返回拦截器的值。

参数

  1. value (*) : 提供给拦截器的值

  2. interceptor (Function) : 待执行的函数

  3. [thisArg] (*) : interceptor 绑定的 this

返回

(*) : 返回 interceptor 的结果

示例

_('  abc  ')
 .chain()
 .trim()
 .thru(function(value) {
   return [value];
 })
 .value();
// → ['abc']

_.prototype.chain()

在包装对象上显式开启链式调用

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var users = [
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 }
];

// 不使用显式调用链
_(users).first();
// → { 'user': 'barney', 'age': 36 }

// 使用显式调用链
_(users).chain()
  .first()
  .pick('user')
  .value();
// → { 'user': 'barney' }

_.prototype.commit()

执行调用链队列并返回包装集结果

返回

(Object) : 返回新的 lodash 包装集实例

示例

var array = [1, 2];
var wrapped = _(array).push(3);

console.log(array);
// → [1, 2]

wrapped = wrapped.commit();
console.log(array);
// → [1, 2, 3]

wrapped.last();
// → 3

console.log(array);
// → [1, 2, 3]

_.prototype.concat([values])

Creates a new array joining a wrapped array with any additional arrays and/or values.

创建一个含有连接其他数组和(或)值的新数组包装集。

参数

  1. [values] (…*) : 带连接的值

返回

(Array) : 返回一个已连接的新数组

示例

var array = [1];
var wrapped = _(array).concat(2, [3], [[4]]);

console.log(wrapped.value());
// → [1, 2, 3, [4]]

console.log(array);
// → [1]

_.prototype.plant()

创建一个链式调用队列的克隆(会将 value 替换为克隆后的链式调用链中)。

返回

(Object) : 返回一个新的 lodash 包装实例

示例

var array = [1, 2];
var wrapped = _(array).map(function(value) {
  return Math.pow(value, 2);
});

var other = [3, 4];
var otherWrapped = wrapped.plant(other);

otherWrapped.value();
// → [9, 16]

wrapped.value();
// → [1, 4]

_.prototype.reverse()

翻转包装数组,将第一个元素和最后一个元素对换,第二个元素和倒数第二个元素对换,以此类推。

注意:该方法会修改包装数组。

返回

(Object) : 返回一个已经翻转过的数组的 lodash 包装实例

示例

var array = [1, 2, 3];

_(array).reverse().value()
// → [3, 2, 1]

console.log(array);
// → [3, 2, 1]

_.prototype.toString()

Produces the result of coercing the unwrapped value to a string.

产生强制将值转为未包装的字符串果。

返回

(string) : 返回强制转为字符串的值

示例

_([1, 2, 3]).toString();
// → '1,2,3'

_.prototype.value()

执行方法链队列并提取未包装的值

别名

  • _.prototype.run

  • _.prototype.toJSON

  • _.prototype.valueOf

返回

(*) : 返回已处理的未包装的值

示例

_([1, 2, 3]).value();
// → [1, 2, 3]


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务