mirror of
https://github.com/jquery/jquery.git
synced 2026-01-23 09:58:27 -05:00
Fix #13075. Optimize $.type by preferring typeof. Close gh-1089.
Also fixes browsers where `typeof RegExp === "function"`.
This commit is contained in:
committed by
Dave Methvin
parent
d829804631
commit
5eec75e582
@@ -427,9 +427,12 @@ jQuery.extend({
|
||||
},
|
||||
|
||||
type: function( obj ) {
|
||||
return obj == null ?
|
||||
String( obj ) :
|
||||
class2type[ core_toString.call(obj) ] || "object";
|
||||
if ( obj == null ) {
|
||||
return String( obj );
|
||||
}
|
||||
return typeof obj === "object" || typeof obj === "function" ?
|
||||
class2type[ core_toString.call(obj) ] || "object" :
|
||||
typeof obj;
|
||||
},
|
||||
|
||||
isPlainObject: function( obj ) {
|
||||
|
||||
@@ -243,7 +243,7 @@ test("trim", function() {
|
||||
});
|
||||
|
||||
test("type", function() {
|
||||
expect( 24 );
|
||||
expect( 28 );
|
||||
|
||||
equal( jQuery.type(null), "null", "null" );
|
||||
equal( jQuery.type(undefined), "undefined", "undefined" );
|
||||
@@ -269,6 +269,16 @@ test("type", function() {
|
||||
equal( jQuery.type(document.body), "object", "Element" );
|
||||
equal( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
|
||||
equal( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
|
||||
|
||||
// Avoid Lint complaints
|
||||
var MyString = String;
|
||||
var MyNumber = Number;
|
||||
var MyBoolean = Boolean;
|
||||
var MyObject = Object;
|
||||
equal( jQuery.type(new MyBoolean(true)), "boolean", "Boolean" );
|
||||
equal( jQuery.type(new MyNumber(1)), "number", "Number" );
|
||||
equal( jQuery.type(new MyString("a")), "string", "String" );
|
||||
equal( jQuery.type(new MyObject()), "object", "Object" );
|
||||
});
|
||||
|
||||
asyncTest("isPlainObject", function() {
|
||||
|
||||
Reference in New Issue
Block a user