简体   繁体   English

Underscore.js _.isElement函数

[英]Underscore.js _.isElement function

I have been looking underscore.js library functions and I noticed a function which returns whether the element is a DOM element or not. 我一直在寻找underscore.js库函数,并且注意到有一个函数返回该元素是否为DOM元素。 The function is below. 该功能如下。

_.isElement = function(obj) {
    return !!(obj && obj.nodeType == 1);
};

Can you please tell me why !! 你能告诉我为什么吗!! is being used instead of just returning (obj && obj.nodeType == 1) . 正在使用而不是仅仅返回(obj && obj.nodeType == 1) I am wondering whether !! 我想知道是否!! adds any performance improvements. 增加了任何性能改进。 Any idea... 任何想法...

!! forces the result to be a boolean. 强制结果为布尔值。

If you pass null , for example, then the && will return null . 例如,如果传递null ,则&&将返回null The !! !! converts that to false . 将其转换为false

If obj is "truthy", you'll get the result of obj.nodeType == 1 which is a boolean. 如果obj是“ truthy”,则将得到obj.nodeType == 1的结果,它是一个布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM