if(isFunction(selector) && selector.call!==undefined){
this.each(function(idx){
if(!selector.call(this,idx)) nodes.push(this)
})
}
这个if语句需要检测两个条件:
1.selector 是不是 Function,下面是isFunction的源码:
function isFunction(value) {
return Object.prototype.toString.call(value) ==
"[object Function]" }
2.判断selector是否具有call方法
分析:
如果isFunction(selector)成立,说明selector是Function的实例,而Function.prototype中包含call方法.那么isFunction(selector)如果为真的话,selector必然有call方法,也就是selector.call!==undefined必然成立.
那么这个逻辑与运算是否多此一举?
PHPz2017-04-10 17:26:09
极端情况
var selector = function(){...};
selector.call = undefined;
> isFunction(selector) && selector.call!==undefined
false
PHPz2017-04-10 17:26:09
underscore中的实现是这样的:
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError.
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error', 'Symbol'], function(name) {
_['is' + name] = function(obj) {
return toString.call(obj) === '[object ' + name + ']';
};
});