(typeof variable === " function ")和jQuery.isFunction()之间有什么区别?

时间:2022-11-05 08:35:52

I have always used (typeof variable === "function") and I stumbled across jQuery.isFunction() and I was wondering:

我一直使用(typeof variable === "function"),我偶然发现了jQuery.isFunction(),我想知道:

  1. What is the difference between the typeof method and jQuery's method? And not only what the difference is, but
  2. 方法的类型和jQuery的方法有什么不同?不仅是区别是什么,而且
  3. When is it appropriate to use the typeof method and when is it appropriate to use jQuery's method?
  4. 什么时候使用类型方法合适,什么时候使用jQuery方法合适?

5 个解决方案

#1


9  

There is almost no 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 exact same 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-JS and check out some of its features :P

在这种情况下,jQuery根本没有任何优势(或者以这种方式,90%的库用例)。看看Vanilla-JS,看看它的一些特点:P

TLDR: Don't use jQuery for this...or anything.

不要用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。

#2


3  

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方法。函数(){}上的原型链,到字符串[对象函数]。这就是它与前面示例的不同之处,以及它比typeof慢的原因。

#3


2  

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时返回true。调用其参数返回[对象函数]。

#4


2  

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

#5


0  

The difference is that JQuery calls something equivalent to the following and checks for the "Function" string token:

不同之处在于JQuery调用与以下内容等价的东西,并检查“函数”字符串标记:

var toString = Object.prototype.toString;
var func = function(){};

console.log(toString.call(func)); // "returns [object Function]"

Whereas typof, just returns "function":

而类型,只返回“function”:

var fType = typeof func; 

console.log(fType); // returns "function"

Here's an example.

这是一个例子。

#1


9  

There is almost no 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 exact same 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-JS and check out some of its features :P

在这种情况下,jQuery根本没有任何优势(或者以这种方式,90%的库用例)。看看Vanilla-JS,看看它的一些特点:P

TLDR: Don't use jQuery for this...or anything.

不要用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。

#2


3  

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方法。函数(){}上的原型链,到字符串[对象函数]。这就是它与前面示例的不同之处,以及它比typeof慢的原因。

#3


2  

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时返回true。调用其参数返回[对象函数]。

#4


2  

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

#5


0  

The difference is that JQuery calls something equivalent to the following and checks for the "Function" string token:

不同之处在于JQuery调用与以下内容等价的东西,并检查“函数”字符串标记:

var toString = Object.prototype.toString;
var func = function(){};

console.log(toString.call(func)); // "returns [object Function]"

Whereas typof, just returns "function":

而类型,只返回“function”:

var fType = typeof func; 

console.log(fType); // returns "function"

Here's an example.

这是一个例子。