[英]Checking if a function is of type Function in javascript
Yes, I know there are plenty of posts on this, but I do not understand the selected answer of this post . 是的,我知道这方面有很多帖子,但是我不理解该帖子的选定答案。 Particularly, why is it necessary to return object
? 特别是为什么要return object
?
Why wouldn't something like Object.prototype.toString.call(myFunc)
be sufficient as described by MDN ? 为什么像MDN所描述的那样, Object.prototype.toString.call(myFunc)
类的东西就不够了?
why is it necessary to
return object
? 为什么必须return object
?
It's not necessary to do object &&
, but a shortcut for falsy values that avoids the method call for values such as null
. 不需要执行object &&
,而是伪造值的快捷方式,它避免了方法调用诸如null
。 Of course, if you're looking for speed, you probably should go for typeof object == "function"
当然,如果您正在寻找速度,则可能应该使用typeof object == "function"
Why wouldn't something like
Object.prototype.toString.call(myFunc)
be sufficient? 为什么像Object.prototype.toString.call(myFunc)
这样的东西不够?
It is sufficient. 足够了。
return object && getClass.call(object) == '[object Function]'
This construction simply verifies if object
is not null
or undefined
(generally falsy). 这种构造只是简单地验证object
是否为null
或undefined
(通常是虚假的)。
This way it's faster, because JavaScript doesn't evaluate the getClass()
call on a falsy object
. 这样,速度更快,因为JavaScript不会评估falsy object
上的getClass()
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.