- _.attempt(func)
- .callback([func=.identity], [thisArg])
- _.constant(value)
- _.identity(value)
- _.matches(source)
- _.matchesProperty(path, srcValue)
- _.method(path, [args])
- _.methodOf(object, [args])
- _.mixin([object=lodash], source, [options])
- _.noConflict()`
- _.noop()
- _.property(path)
- _.propertyOf(object)
- _.range([start=0], end, [step=1])
- _.runInContext([context=root])
- .times(n, [iteratee=.identity], [thisArg])
- _.uniqueId([prefix])
_.attempt(func)
Attempts to invoke func, returning either the result or the caught error
object. Any additional arguments are provided to func when it’s invoked.
Arguments
- func(Function): The function to attempt.
Returns
(*):  Returns the func result or error object.
Example
// avoid throwing errors for invalid selectors
var elements = _.attempt(function(selector) {
return document.querySelectorAll(selector);
}, '>_>');
if (_.isError(elements)) {
elements = [];
}
.callback([func=.identity], [thisArg])
Creates a function that invokes func with the this binding of thisArg
and arguments of the created function. If func is a property name the
created callback returns the property value for a given element. If func
is an object the created callback returns true for elements that contain
the equivalent object properties, otherwise it returns false.
Aliases
_.iteratee
Arguments
- [func=_.identity](*): The value to convert to a callback.
- [thisArg](*): The- thisbinding of- func.
Returns
(Function): Returns the callback.
Example
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
// wrap to create custom callback shorthands
_.callback = _.wrap(_.callback, function(callback, func, thisArg) {
var match = /^(.+?)__([gl]t)(.+)$/.exec(func);
if (!match) {
return callback(func, thisArg);
}
return function(object) {
return match[2] == 'gt'
? object[match[1]] > match[3]
: object[match[1]] < match[3];
};
});
_.filter(users, 'age__gt36');
// => [{ 'user': 'fred', 'age': 40 }]
_.constant(value)
Creates a function that returns value.
Arguments
- value(*): The value to return from the new function.
Returns
(Function): Returns the new function.
Example
var object = { 'user': 'fred' };
var getter = _.constant(object);
getter() === object;
// => true
_.identity(value)
This method returns the first argument provided to it.
Arguments
- value(*): Any value.
Returns
(*):  Returns value.
Example
var object = { 'user': 'fred' };
_.identity(object) === object;
// => true
_.matches(source)
Creates a function that performs a deep comparison between a given object
and source, returning true if the given object has equivalent property
values, else false.
Note: This method supports comparing arrays, booleans, Date objects,
numbers, Object objects, regexes, and strings. Objects are compared by
their own, not inherited, enumerable properties. For comparing a single
own or inherited property value see _.matchesProperty.
Arguments
- source(Object): The object of property values to match.
Returns
(Function): Returns the new function.
Example
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, _.matches({ 'age': 40, 'active': false }));
// => [{ 'user': 'fred', 'age': 40, 'active': false }]
_.matchesProperty(path, srcValue)
Creates a function that compares the property value of path on a given
object to value.
Note: This method supports comparing arrays, booleans, Date objects,
numbers, Object objects, regexes, and strings. Objects are compared by
their own, not inherited, enumerable properties.
Arguments
- path(Array|string): The path of the property to get.
- srcValue(*): The value to match.
Returns
(Function): Returns the new function.
Example
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
_.find(users, _.matchesProperty('user', 'fred'));
// => { 'user': 'fred' }
_.method(path, [args])
Creates a function that invokes the method at path on a given object.
Any additional arguments are provided to the invoked method.
Arguments
- path(Array|string): The path of the method to invoke.
- [args](…*): The arguments to invoke the method with.
Returns
(Function): Returns the new function.
Example
var objects = [
{ 'a': { 'b': { 'c': _.constant(2) } } },
{ 'a': { 'b': { 'c': _.constant(1) } } }
];
_.map(objects, _.method('a.b.c'));
// => [2, 1]
_.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
// => [1, 2]
_.methodOf(object, [args])
The opposite of _.method; this method creates a function that invokes
the method at a given path on object. Any additional arguments are
provided to the invoked method.
Arguments
- object(Object): The object to query.
- [args](…*): The arguments to invoke the method with.
Returns
(Function): Returns the new function.
Example
var array = _.times(3, _.constant),
object = { 'a': array, 'b': array, 'c': array };
_.map(['a[2]', 'c[0]'], _.methodOf(object));
// => [2, 0]
_.map([['a', '2'], ['c', '0']], _.methodOf(object));
// => [2, 0]
_.mixin([object=lodash], source, [options])
Adds all own enumerable function properties of a source object to the
destination object. If object is a function then methods are added to
its prototype as well.
Note: Use _.runInContext to create a pristine lodash function to
avoid conflicts caused by modifying the original.
Arguments
- [object=lodash](Function|Object): The destination object.
- source(Object): The object of functions to add.
- [options](Object): The options object.
- [options.chain=true](boolean): Specify whether the functions added are chainable.
Returns
(Function|Object):  Returns object.
Example
function vowels(string) {
return _.filter(string, function(v) {
return /[aeiou]/i.test(v);
});
}
_.mixin({ 'vowels': vowels });
_.vowels('fred');
// => ['e']
_('fred').vowels().value();
// => ['e']
_.mixin({ 'vowels': vowels }, { 'chain': false });
_('fred').vowels();
// => ['e']
_.noConflict()`
Reverts the _ variable to its previous value and returns a reference to
the lodash function.
Returns
(Function):  Returns the lodash function.
Example
var lodash = _.noConflict();
_.noop()
A no-operation function that returns undefined regardless of the
arguments it receives.
Example
var object = { 'user': 'fred' };
_.noop(object) === undefined;
// => true
_.property(path)
Creates a function that returns the property value at path on a
given object.
Arguments
- path(Array|string): The path of the property to get.
Returns
(Function): Returns the new function.
Example
var objects = [
{ 'a': { 'b': { 'c': 2 } } },
{ 'a': { 'b': { 'c': 1 } } }
];
_.map(objects, _.property('a.b.c'));
// => [2, 1]
_.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
// => [1, 2]
_.propertyOf(object)
The opposite of _.property; this method creates a function that returns
the property value at a given path on object.
Arguments
- object(Object): The object to query.
Returns
(Function): Returns the new function.
Example
var array = [0, 1, 2],
object = { 'a': array, 'b': array, 'c': array };
_.map(['a[2]', 'c[0]'], _.propertyOf(object));
// => [2, 0]
_.map([['a', '2'], ['c', '0']], _.propertyOf(object));
// => [2, 0]
_.range([start=0], end, [step=1])
Creates an array of numbers (positive and/or negative) progressing from
start up to, but not including, end. If end is not specified it’s
set to start with start then set to 0. If end is less than start
a zero-length range is created unless a negative step is specified.
Arguments
- [start=0](number): The start of the range.
- end(number): The end of the range.
- [step=1](number): The value to increment or decrement by.
Returns
(Array): Returns the new array of numbers.
Example
_.range(4);
// => [0, 1, 2, 3]
_.range(1, 5);
// => [1, 2, 3, 4]
_.range(0, 20, 5);
// => [0, 5, 10, 15]
_.range(0, -4, -1);
// => [0, -1, -2, -3]
_.range(1, 4, 0);
// => [1, 1, 1]
_.range(0);
// => []
_.runInContext([context=root])
Create a new pristine lodash function using the given context object.
Arguments
- [context=root](Object): The context object.
Returns
(Function):  Returns a new lodash function.
Example
_.mixin({ 'foo': _.constant('foo') });
var lodash = _.runInContext();
lodash.mixin({ 'bar': lodash.constant('bar') });
_.isFunction(_.foo);
// => true
_.isFunction(_.bar);
// => false
lodash.isFunction(lodash.foo);
// => false
lodash.isFunction(lodash.bar);
// => true
// using `context` to mock `Date#getTime` use in `_.now`
var mock = _.runInContext({
'Date': function() {
return { 'getTime': getTimeMock };
}
});
// or creating a suped-up `defer` in Node.js
var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
.times(n, [iteratee=.identity], [thisArg])
Invokes the iteratee function n times, returning an array of the results
of each invocation. The iteratee is bound to thisArg and invoked with
one argument; (index).
Arguments
- n(number): The number of times to invoke- iteratee.
- [iteratee=_.identity](Function): The function invoked per iteration.
- [thisArg](*): The- thisbinding of- iteratee.
Returns
(Array): Returns the array of results.
Example
var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
// => [3, 6, 4]
_.times(3, function(n) {
mage.castSpell(n);
});
// => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
_.times(3, function(n) {
this.cast(n);
}, mage);
// => also invokes `mage.castSpell(n)` three times
_.uniqueId([prefix])
Generates a unique ID. If prefix is provided the ID is appended to it.
Arguments
- [prefix](string): The value to prefix the ID with.
Returns
(string): Returns the unique ID.
Example
_.uniqueId('contact_');
// => 'contact_104'
_.uniqueId();
// => '105'
 我的书签
 我的书签
                                 添加书签
 添加书签 移除书签
 移除书签