result
_.result(object,property)
- 如果参数object是function,执行它
- 其他的情况直接返回
官方实例:
var object = { cheese:"crumpets", stuff:function(){ return "nonsense"; } }; _.result(object,"cheese"); //"crumpets" _.result(object,"stuff"); //"nonsense"
源码解析:
/* * @name result * @param object * @param property * @info 依赖_.isFunction */ _.result = function(object,property){ //对null的判断 if(object == null){ return null; } var value = object[property]; //判断是否是函数,如果是函数call,如果其他直接拿key去匹配 return _.isFunction(value) ? value.call(object) : value; }