Object Functions
keys_.keys(object)
Retrieve all the names of the object's properties.
_.keys({one : 1, two : 2, three : 3}); => ["one", "two", "three"]
得到对象所有属性名称
values_.values(object)
Return all of the values of the object's properties.
_.values({one : 1, two : 2, three : 3}); => [1, 2, 3]
得到对象所有值
pairs_.pairs(object)
Convert an object into a list of [key, value] pairs.
_.pairs({one: 1, two: 2, three: 3}); => [["one", 1], ["two", 2], ["three", 3]]
把对象转换成为键值对
invert_.invert(object)
Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object's values should be unique and string serializable.
_.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"}); => {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};
把对象的Key,value倒置,要让它工作,对象的所有值必须唯一且可以序列化.
functions_.functions(object)
Alias: methods
Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object.
_.functions(_); => ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...
返回对象的所有函数名(方法名)
extend_.extend(destination, *sources)
Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
_.extend({name : 'moe'}, {age : 50}); => {name : 'moe', age : 50}
扩展,复制源对象的所有属性到目标对象,返回目标对象.如果有同名的则覆盖
pick_.pick(object, *keys)
Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys).
_.pick({name : 'moe', age: 50, userid : 'moe1'}, 'name', 'age'); => {name : 'moe', age : 50}
挑选,返回仅包含白名单Key的对象拷贝
omit_.omit(object, *keys)
Return a copy of the object, filtered to omit the blacklisted keys (or array of keys).
_.omit({name : 'moe', age : 50, userid : 'moe1'}, 'userid'); => {name : 'moe', age : 50}
忽略,返回忽略掉黑名单Key的对象拷贝
defaults_.defaults(object, *defaults)
Fill in null and undefined properties in object with values from the defaults objects, and return the object. As soon as the property is filled, further defaults will have no effect.
var iceCream = {flavor : "chocolate"}; _.defaults(iceCream, {flavor : "vanilla", sprinkles : "lots"}); => {flavor : "chocolate", sprinkles : "lots"}
默认值,从默认对象里来填充对象为空或未定义的属性的值,并返回对象,如果对象已经有值将不会产生影响
clone_.clone(object)
Create a shallow-copied clone of the object. Any nested objects or arrays will be copied by reference, not duplicated.
_.clone({name : 'moe'}); => {name : 'moe'};
克隆,浅克隆对象,所有嵌套的对象或属性都被复制成引用.
tap_.tap(object, interceptor)
Invokes interceptor with the object, and then returns object. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
_.chain([1,2,3,200]) .filter(function(num) { return num % 2 == 0; }) .tap(alert) .map(function(num) { return num * num }) .value(); => // [2, 200] (alerted) => [4, 40000]
调用拦截的对象,然后返回对象。此方法的主要目的是“进入”的方法的链中,为了内链上执行操作的中间结果。
has_.has(object, key)
Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it's been overridden accidentally.
_.has({a: 1, b: 2, c: 3}, "b"); => true
判断对象是否包含Key,它更安全没有意外的覆盖情况
isEqual_.isEqual(object, other)
Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.
var moe = {name : 'moe', luckyNumbers : [13, 27, 34]}; var clone = {name : 'moe', luckyNumbers : [13, 27, 34]}; moe == clone; => false _.isEqual(moe, clone); => true
是否相等,在两个对象间执行优化后的深度对比, 以确定它们是否应该被认为是相等的。
isEmpty_.isEmpty(object)
Returns true if object contains no values.
_.isEmpty([1, 2, 3]); => false _.isEmpty({}); => true
是否为空:对果对象没有值返回true.
isElement_.isElement(object)
Returns true if object is a DOM element.
_.isElement(jQuery('body')[0]); => true
是否元素:如果对象是Dom元素返回true
isArray_.isArray(object)
Returns true if object is an Array.
(function(){ return _.isArray(arguments); })(); => false _.isArray([1,2,3]); => true
是否数组
isObject_.isObject(value)
Returns true if value is an Object. Note that JavaScript arrays and functions are objects, while (normal) strings and numbers are not.
_.isObject({}); => true _.isObject(1); => false
是否对象
isArguments_.isArguments(object)
Returns true if object is an Arguments object.
(function(){ return _.isArguments(arguments); })(1, 2, 3); => true _.isArguments([1,2,3]); => false
是否参数
isFunction_.isFunction(object)
Returns true if object is a Function.
_.isFunction(alert); => true
是否函数
isString_.isString(object)
Returns true if object is a String.
_.isString("moe"); => true
是否字符串
isNumber_.isNumber(object)
Returns true if object is a Number (including NaN).
_.isNumber(8.4 * 5); => true
是否数值 (including NaN).
isFinite_.isFinite(object)
Returns true if object is a finite Number.
_.isFinite(-101); => true _.isFinite(-Infinity); => false
是否有限数值
isBoolean_.isBoolean(object)
Returns true if object is either true or false.
_.isBoolean(null); => false
是否布尔值
isDate_.isDate(object)
Returns true if object is a Date.
_.isDate(new Date()); => true
是否日期
isRegExp_.isRegExp(object)
Returns true if object is a RegExp.
_.isRegExp(/moe/); => true
是否正则表达式
isNaN_.isNaN(object)
Returns true if object is NaN.
Note: this is not the same as the native isNaN function, which will also return true if the variable is undefined.
_.isNaN(NaN); => true isNaN(undefined); => true _.isNaN(undefined); => false
是否NAN
isNull_.isNull(object)
Returns true if the value of object is null.
_.isNull(null); => true _.isNull(undefined); => false
是否为Null
isUndefined_.isUndefined(value)
Returns true if value is undefined.
_.isUndefined(window.missingVariable); => true
是否Undefined