Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Core: jQuery.isFunction returns false for Function objects with custom @@toStringTag #3600
Comments
akihikodaki
changed the title from
Core: jQuery.isFunction returns false for objects inheriting Function objects.
to
Core: jQuery.isFunction returns false for Function objects with custom @@toStringTag
Apr 1, 2017
akihikodaki
referenced this issue
in jquery/api.jquery.com
Apr 1, 2017
Closed
jQuery.isFunction: ambiguous criteria #1034
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Krinkle
Apr 1, 2017
Member
@gibson042 From a quick check seems like patching $.type() to align more with typeof should otherwise be harmless. We've already been going in that direction anyways.
As of 3.2, we already do this for most cases. We now only use class2type[toString] for values where typeof returns function or object.
If we remove function from that, reducing use of class2type to object only, should do the trick.
Although we may need to find another way to support Android 2.3's RegExp values in that case.
type: function( obj ) {
if ( obj == null ) {
return obj + "";
}
// Support: Android <=2.3 only (functionish RegExp)
return typeof obj === "object" || typeof obj === "function" ?
class2type[ toString.call( obj ) ] || "object" :
typeof obj;
},
isFunction: function( obj ) {
return jQuery.type( obj ) === "function";
},|
@gibson042 From a quick check seems like patching As of 3.2, we already do this for most cases. We now only use If we remove Although we may need to find another way to support Android 2.3's RegExp values in that case. type: function( obj ) {
if ( obj == null ) {
return obj + "";
}
// Support: Android <=2.3 only (functionish RegExp)
return typeof obj === "object" || typeof obj === "function" ?
class2type[ toString.call( obj ) ] || "object" :
typeof obj;
},
isFunction: function( obj ) {
return jQuery.type( obj ) === "function";
}, |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mgol
Apr 1, 2017
Member
|
We don't support Android 2.3 anymore, 4.0 is our minimum.
--
Michał Gołębiowski
|
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
akihikodaki
Apr 2, 2017
That breaks for some cases such as document.createElement( "object" ). It is/was actually a function (callable object), but jQuery.isFunction( document.createElement( "object" ) ) is expected to be false.
obj = document.createElement( "object" );
// Firefox says this is a function
assert.ok( !jQuery.isFunction( obj ), "Object Element" );Line 442 in ac9e301
On the other hand, the documentation says:
Determine the internal JavaScript [[Class]] of an object.
Though [[Class]] does no longer exist since ECMAScript 2015, it is similar to @@toStringTag and people may consider jQuery.type derives from that. The change regarding GeneratorFunction as function breaks this expectation.
We need to make clear what we expect for jQuery.isFunction and jQuery.type rather than considering only GeneratorFunction to prevent such confusions.
akihikodaki
commented
Apr 2, 2017
|
That breaks for some cases such as obj = document.createElement( "object" );
// Firefox says this is a function
assert.ok( !jQuery.isFunction( obj ), "Object Element" );Line 442 in ac9e301 On the other hand, the documentation says:
Though We need to make clear what we expect for |
This was referenced Apr 2, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
gibson042
Apr 2, 2017
Member
I'd rather deprecate jQuery.type than try to keep up with external changes. jQuery.isFunction doesn't need to rely on it, and shouldn't do so any longer IMO.
|
I'd rather deprecate |
gibson042
added
the
Core
label
Apr 2, 2017
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mgol
Apr 2, 2017
Member
@gibson042 if all jQuery.isFunction is going to do is checking if typeof is "function" then we should deprecate it as well.
|
@gibson042 if all |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
gibson042
Apr 2, 2017
Member
I'm fine with deprecating it, but unfortunately it's a little more than just typeof because typeof document.createElement("object") === "function" in Firefox.
|
I'm fine with deprecating it, but unfortunately it's a little more than just |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mgol
Apr 2, 2017
Member
typeof document.createElement("object") === "function" in Firefox
Not just in Firefox, in Chrome it's "function", too. And in both of them it's changed to "object" in a beta version. I couldn't find any other browser (among these we support) where it's "function" so it will soon stop being an issue. Unless we want to be extra-safe here that nothing DOM-based returns true in jQuery.isFunction.
What's funny is in Chrome it used be "object" in versions <=46.
Not just in Firefox, in Chrome it's What's funny is in Chrome it used be |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
akihikodaki
Apr 3, 2017
It's a DOM specific problem, so why not check if it is an element in the functions which deals with DOM? It is not reasonable to do that in jQuery.isFunction.
akihikodaki
commented
Apr 3, 2017
•
|
It's a DOM specific problem, so why not check if it is an element in the functions which deals with DOM? It is not reasonable to do that in |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
dmethvin
Apr 3, 2017
Member
I'm with @gibson042 that we should discourage the use of these utility functions and not set the expectation they'll be updated for bizarre usage. If someone passes a generator function to jQuery they're doing something mighty strange. DO NOT TAUNT HAPPY FUN JQUERY!
|
I'm with @gibson042 that we should discourage the use of these utility functions and not set the expectation they'll be updated for bizarre usage. If someone passes a generator function to jQuery they're doing something mighty strange. DO NOT TAUNT HAPPY FUN JQUERY! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Krinkle
Apr 3, 2017
Member
If we plan to deprecate these methods, that means people will eventually need to migrate use to typeof, isArray, and other native checks. In that case I propose we keep them unchanged and deprecate them in their current form. There's little point in improving them further to allow new usage of patterns only to require migration later. It's certainly less risky in terms of back-compat and more predictable / easier to document ([[Class]] / @@toStringTag).
|
If we plan to deprecate these methods, that means people will eventually need to migrate use to |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
akihikodaki
Apr 4, 2017
There's little point in improving them further to allow new usage of patterns only to require migration later.
Closing this issue.
akihikodaki
commented
Apr 4, 2017
Closing this issue. |
akihikodaki commentedApr 1, 2017
•
edited
Edited 1 time
-
akihikodaki
edited Apr 1, 2017
Description
jQuery.isFunctionreturns false for Function objects with custom@@toStringTag. That is undesirable in some case such asGeneratorFunction.This flaw was pointed out at #3597 (comment).
Link to test case
https://jsbin.com/xocanozuye/1/edit?html,js,console