javascript (typeof variable === "function") 和 jQuery.isFunction() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12206083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between (typeof variable === "function") and jQuery.isFunction()?
提问by Aust
I have always used (typeof variable === "function")
and I stumbled across jQuery.isFunction()
and I was wondering:
我一直在使用(typeof variable === "function")
,我偶然发现jQuery.isFunction()
,我想知道:
- What is the difference between the typeof method and jQuery's method? And not only what the difference is, but
- When is it appropriate to use the typeof method and when is it appropriate to use jQuery's method?
- typeof方法和jQuery的方法有什么区别?不仅有什么不同,还有
- 什么时候用typeof方法合适,什么时候用jQuery的方法合适?
回答by Lusitanian
There is almostno difference, other than that using jQuery is slightly slower. See the source code:
目前几乎没有区别,比使用jQuery等稍慢。查看源代码:
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},
which calls a function which calls another function to determine the exactsame thing as what you showed :P
它调用一个函数,该函数调用另一个函数来确定与您显示的内容完全相同的内容:P
There is literally no advantage to jQuery in this case [or for that manner, 90% of the use-cases of the library]. Look into Vanilla-JSand check out some of its features :P
在这种情况下,jQuery 几乎没有任何优势 [或者对于这种情况,库的 90% 的用例]。查看Vanilla-JS并查看它的一些功能:P
TLDR: Don't use jQuery for this...or anything.
TLDR:不要为此使用jQuery……或任何东西。
UPDATE
更新
Here's a benchmark showing you that Vanilla JS is roughly 93% faster than jQuery: http://jsperf.com/jquery-isfunction-vs-vanilla-is-function.
这是一个基准,向您展示 Vanilla JS 比 jQuery 快大约 93%:http: //jsperf.com/jquery-isfunction-vs-vanilla-is-function。
回答by 0x499602D2
There's no difference. jQuery uses the same concept:
没有区别。jQuery 使用相同的概念:
// ...
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
}
Update:
更新:
After digging in deeper I found that jQuery's isFunction
method is comparing the toString
method of the Object.prototype
chain on function() {}
, to the string [object Function]
. This is the how it differs from your former example and the reason for it being slower than typeof
.
在深入挖掘之后,我发现 jQuery 的isFunction
方法是将链上的toString
方法与字符串进行比较。这就是它与您之前的示例的不同之处以及它比.Object.prototype
function() {}
[object Function]
typeof
回答by JohnB
The jQuery source code for isFunction is
isFunction 的 jQuery 源代码是
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},
type: function( obj ) {
return obj == null ?
String( obj ) :
class2type[ core_toString.call(obj) ] || "object";
},
//...
jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "),
function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
core_toString = Object.prototype.toString,
hence jQuery.isFunction
returns true if and only if calling Object.prototype.toString.call
on its argument returns [object Function]
.
因此jQuery.isFunction
当且仅当调用Object.prototype.toString.call
其参数返回true [object Function]
。
回答by Yuan Yuan
Usually you can test whether JS object is function by using this test:
通常你可以通过这个测试来测试JS对象是否是函数:
(typeof fn === 'function')
However, this doesn't always work (IE8):
但是,这并不总是有效(IE8):
typeof alert => 'object'
typeof document.createElement('input').getAttribute => 'object'
Before jQuery 1.4 internally they used the same check, but now they've fixed it. So to be sure that passed object is a function which can be called, just use $.isFunction method:
在 jQuery 1.4 之前,他们在内部使用相同的检查,但现在他们已经修复了它。所以要确保传递的对象是一个可以调用的函数,只需使用 $.isFunction 方法:
$.isFunction(function() {}) => true
$.isFunction(alert) => true
$.isFunction(document.createElement('input').getAttribute) => true
Copied from this blog: http://nuald.blogspot.co.uk/2012/07/why-jqueryisfunction.html
复制自此博客:http: //nuald.blogspot.co.uk/2012/07/why-jqueryisfunction.html
回答by Greg Ross
The difference is that JQuery calls something equivalent to the following and checks for the "Function" string token:
不同之处在于 JQuery 调用与以下等效的内容并检查“Function”字符串标记:
var toString = Object.prototype.toString;
var func = function(){};
console.log(toString.call(func)); // "returns [object Function]"
Whereas typof, just returns "function":
而typof,只返回“函数”:
var fType = typeof func;
console.log(fType); // returns "function"