## _
+ [link](./_ "Link to this entry.")
+ [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L1494 "View in source.")
+ [npm](https://www.npmjs.com/package/lodash._ "See the npm package.")
```
_(value)
```
创建一个经 `lodash` 包装后的对象会启用隐式链。返回的数组、集合、方法相互之间能够链式调用。 检索唯一值或返回原始值会自动解除链条并返回计算后的值,否则需要调用 `_#value` 方法解除链(即获得计算结果)。
显式链式调用,在任何情况下需要先用 `_#value` 解除链后,才能使用 `_.chain` 开启。
链式方法是惰性计算的,直到隐式或者显式调用了 `_#value` 才会执行计算。
惰性计算接受几种支持 shortcut fusion 的方法, shortcut fusion 是一种通过合并链式 iteratee 调用从而大大降低迭代的次数以提高执行性能的方式。 部分链有资格 shortcut fusion,如果它至少有超过二百个元素的数组和任何只接受一个参数的 iteratees。 触发的方式是任何一个 shortcut fusion 有了变化。
链式方法支持定制版本,只要 `_#value` 包含或者间接包含在版本中。
除了 lodash 的自身方法,包装后的对象还支持 `Array` 的 `String` 的方法。
支持 `Array` 的方法:
`concat`, `join`, `pop`, `push`, `shift`, `sort`, `splice`, 以及 `unshift`
支持 `String` 的方法:
`replace` 以及 `split`
支持 shortcut fusion 的方法:
`at`, `compact`, `drop`, `dropRight`, `dropWhile`, `filter`, `find`, `findLast`, `head`, `initial`, `last`, `map`, `reject`, `reverse`, `slice`, `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, 以及 `toArray`
默认不支持 链式调用 的方法:
`add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`, `cloneDeep`, `cloneDeepWith`, `cloneWith`, `deburr`, `endsWith`, `eq`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `floor`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `get`, `gt`, `gte`, `has`, `hasIn`, `head`, `identity`, `includes`, `indexOf`, `inRange`, `invoke`, `isArguments`, `isArray`, `isArrayBuffer`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`, `isBuffer`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`, `isError`, `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMap`, `isMatch`, `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`, `isPlainObject`, `isRegExp`, `isSafeInteger`, `isSet`, `isString`, `isUndefined`, `isTypedArray`, `isWeakMap`, `isWeakSet`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`, `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`, `noConflict`, `noop`, `now`, `pad`, `padEnd`, `padStart`, `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `subtract`, `sum`, `sumBy`, `template`, `times`, `toLower`, `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, `toString`, `toUpper`, `trim`, `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`, `upperCase`, `upperFirst`, `value`, 以及 `words`
支持 链式调用 的方法:
`after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, `at`, `before`, `bind`, `bindAll`, `bindKey`, `castArray`, `chain`, `chunk`, `commit`, `compact`, `concat`, `conforms`, `constant`, `countBy`, `create`, `curry`, `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, `difference`, `differenceBy`, `differenceWith`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, `flattenDepth`, `flip`, `flow`, `flowRight`, `fromPairs`, `functions`, `functionsIn`, `groupBy`, `initial`, `intersection`, `intersectionBy`, `intersectionWith`, `invert`, `invertBy`, `invokeMap`, `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, `method`, `methodOf`, `mixin`, `negate`, `nthArg`, `omit`, `omitBy`, `once`, `orderBy`, `over`, `overArgs`, `overEvery`, `overSome`, `partial`, `partialRight`, `partition`, `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, `pullAllBy`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`, `reject`, `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`, `spread`, `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`, `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, `zipObject`, `zipObjectDeep`, 以及 `zipWith`
### 参数
1. value (\*)
需要被包装为 `lodash` 实例的值.
### 返回值 (Object)
返回 `lodash` 包装后的实例
### 示例
```
function square(n) {
return n * n;
}
var wrapped = _([1, 2, 3]);
// 返回未包装的值
wrapped.reduce(_.add);
// => 6
// 返回链式包装的值
var squares = wrapped.map(square);
_.isArray(squares);
// => false
_.isArray(squares.value());
// => true
```
- 主页
- 入门指南
- 下载
- 引入
- 模块格式
- 深入了解
- 兼容性
- 版本定制
- Array
- chunk
- compact
- concat
- difference
- differenceBy
- differenceWith
- drop
- dropRight
- dropRightWhile
- dropWhile
- fill
- findIndex
- findLastIndex
- flatten
- flattenDeep
- flattenDepth
- fromPairs
- head first
- indexOf
- initial
- intersection
- intersectionBy
- intersectionWith
- join
- last
- lastIndexOf
- prototype.reverse
- pull
- pullAll
- pullAllBy
- pullAt
- remove
- slice
- sortedIndex
- sortedIndexBy
- sortedIndexOf
- sortedLastIndex
- sortedLastIndexBy
- sortedLastIndexOf
- sortedUniq
- sortedUniqBy
- tail
- take
- takeRight
- takeRightWhile
- takeWhile
- union
- unionBy
- unionWith
- uniq
- uniqBy
- uniqWith
- unzip
- unzipWith
- without
- xor
- xorBy
- xorWith
- zip
- zipObject
- zipObjectDeep
- zipWith
- Collection
- countBy
- every
- filter
- find
- findLast
- flatMap
- forEach each
- forEachRight eachRight
- groupBy
- includes
- invokeMap
- keyBy
- map
- orderBy
- partition
- reduce
- reduceRight
- reject
- sample
- sampleSize
- shuffle
- size
- some
- sortBy
- Date
- now
- Function
- after
- ary
- before
- bind
- bindKey
- curry
- curryRight
- debounce
- defer
- delay
- flip
- memoize
- negate
- once
- overArgs
- partial
- partialRight
- rearg
- rest
- spread
- throttle
- unary
- wrap
- Lang
- castArray
- clone
- cloneDeep
- cloneDeepWith
- cloneWith
- eq
- gt
- gte
- isArguments
- isArray
- isArrayBuffer
- isArrayLike
- isArrayLikeObject
- isBoolean
- isBuffer
- isDate
- isElement
- isEmpty
- isEqual
- isEqualWith
- isError
- isFinite
- isFunction
- isInteger
- isLength
- isMap
- isMatch
- isMatchWith
- isNaN
- isNative
- isNil
- isNull
- isNumber
- isObject
- isObjectLike
- isPlainObject
- isRegExp
- isSafeInteger
- isSet
- isString
- isSymbol
- isTypedArray
- isUndefined
- isWeakMap
- isWeakSet
- lt
- lte
- toArray
- toInteger
- toLength
- toNumber
- toPlainObject
- toSafeInteger
- toString
- Math
- add
- ceil
- floor
- max
- maxBy
- mean
- min
- minBy
- round
- subtract
- sum
- sumBy
- Methods
- templateSettings.imports._
- Number
- clamp
- inRange
- random
- Object
- assign
- assignIn extend
- assignInWith extendWith
- assignWith
- at
- create
- defaults
- defaultsDeep
- findKey
- findLastKey
- forIn
- forInRight
- forOwn
- forOwnRight
- functions
- functionsIn
- get
- has
- hasIn
- invert
- invertBy
- invoke
- keys
- keysIn
- mapKeys
- mapValues
- merge
- mergeWith
- omit
- omitBy
- pick
- pickBy
- result
- set
- setWith
- toPairs
- toPairsIn
- transform
- unset
- values
- valuesIn
- Properties
- templateSettings
- templateSettings.escape
- templateSettings.evaluate
- templateSettings.imports
- templateSettings.interpolate
- templateSettings.variable
- VERSION
- Seq
- _
- chain
- prototype.at
- prototype.chain
- prototype.commit
- prototype.next
- prototype.plant
- prototype.Symbol.iterator
- prototype.value run, toJSON, valueOf
- tap
- thru
- wrapperFlatMap
- String
- camelCase
- capitalize
- deburr
- endsWith
- escape
- escapeRegExp
- kebabCase
- lowerCase
- lowerFirst
- pad
- padEnd
- padStart
- parseInt
- repeat
- replace
- snakeCase
- split
- startCase
- startsWith
- template
- toLower
- toUpper
- trim
- trimEnd
- trimStart
- truncate
- unescape
- upperCase
- upperFirst
- words
- Util
- attempt
- bindAll
- cond
- conforms
- constant
- flow
- flowRight
- identity
- iteratee
- matches
- matchesProperty
- method
- methodOf
- mixin
- noConflict
- noop
- nthArg
- over
- overEvery
- overSome
- property
- propertyOf
- range
- rangeRight
- runInContext
- times
- toPath
- uniqueId