文档:www.lodashjs.com/docs/lodash…
_.assign(object, [sources]) 将下一个对象的属性(可枚举),分配给前一个对象
function Foo() {
this.a = 1;
}
function Bar() {
this.c = 3;
}
Foo.prototype.b = 2;
Bar.prototype.d = 4;
const assign = _.assign({a: 0}, new Foo, new Bar, {b: 8});
// => {a: 1, c: 3, b: 8} 'assign'
_.assignIn(object, [sources]) 遍历并继承来源对象的属性
function Foo() {
this.a = 1;
}
function Bar() {
this.c = 3;
}
Foo.prototype.b = 2;
Bar.prototype.d = 4;
_.assignIn(new Foo, new Bar);
// => { 'a': 1, 'c': 3, 'd': 4 }
_.at(object, [paths]) 返回一个数组,值为依据paths在object中查找
var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
_.at(object, ['a[0].b.c', 'a[1]']);
// => [3, 4]
var object = { 'a': [1, 4], b: 'bb' };
const at = _.at(object, ['a', 'b']); // => [Array(2), 'bb'] 'at'
_.create(prototype, [properties])创建一个继承 prototype 的对象。 如果提供了 prototype,它的可枚举属性会被分配到创建的对象上
function Shape() {
this.x = 0;
this.y = 0;
}
function Circle() {
Shape.call(this);
}
Circle.prototype = _.create(Shape.prototype, {
'constructor': Circle
});
var circle = new Circle;
circle instanceof Circle;
// => true
circle instanceof Shape;
// => true
_.defaults(object, [sources]) 自左向右执行,第二个及以后的参数,向第一个参数对象中添加(重复属性忽略),返回一个对象
_.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
// => { 'a': 1, 'b': 2 }
_.defaultsDeep(object, [sources]) 类似_.defaults,除了它会递归分配默认属性
_.defaultsDeep({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } });
// => { 'a': { 'b': 2, 'c': 3 } }
_.findKey(object, [predicate=_.identity]) 传入一个对象,返回迭代函数判断第一个符合条件的key
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'
_.findLastKey(object, [predicate=_.identity]) 类似_.findKey反向遍历
_.forIn(object, [iteratee=_.identity]) 遍历对象(object)的自身和继承的可枚举属性,当返回false会结束遍历
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.forIn(new Foo, function(value, key) {
console.log(key);
});
// => Logs 'a', 'b', then 'c' (无法保证遍历的顺序)。
_.forInRight(object, [iteratee=_.identity]) 类似_.forIn。 反方向开始遍历,输出结果的顺序也是反方向
_.forOwn(object, [iteratee=_.identity]) 用法类似_.forIn,但只遍历自身属性,继承属性不进行遍历
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.forOwn(new Foo, function(value, key) {
console.log(key);
});
// => 输出 'a' 然后 'b' (无法保证遍历的顺序)。
_.forOwnRight(object, [iteratee=_.identity]) _.forOwn的反方向遍历,结果顺序也是反方向
_.functions(object) 返回object自身属性名组成的数组
实用函数:_.constant(value) 返回value函数(常量函数)
function Foo() {
this.a = _.constant('a');
this.b = _.constant('b');
}
Foo.prototype.c = _.constant('c');
_.functions(new Foo);
// => ['a', 'b']
_.functionsIn(object)类似_.functions() 返回object自身属性名和继承属性名组成的数组
function Foo() {
this.a = _.constant('a');
this.b = _.constant('b');
}
Foo.prototype.c = _.constant('c');
_.functionsIn(new Foo);
// => ['a', 'b', 'c']