为什么 lodash 没有 result方法在这种情况下返回默认值?
Arguments object (Object): The object to query.
key (string): The key of the property to resolve.
[defaultValue] (*): The value returned if the property value resolves to undefined.
var result = _.result({ foo: 1 }, 'bar', 'default');
console.log(typeof _.result({ foo: 1 }, 'bar') === 'undefined') // true
console.log(result); // expected: 'default'
最佳答案
default
参数好像是3.0.0版本才加入的
比较_.result
实现:
3.0.0 lodash.js
function result(object, key, defaultValue) {
var value = object == null ? undefined : object[key];
if (typeof value == 'undefined') {
value = defaultValue;
}
return isFunction(value) ? value.call(object) : value;
}
和2.2.1 lodash.js :
function result(object, property) {
if (object) {
var value = object[property];
return isFunction(value) ? object[property]() : value;
}
}
关于javascript - Lodash _.result() 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28477363/