From 5eec75e582d27e2d4f625f6be33eabad84f16239 Mon Sep 17 00:00:00 2001 From: Sebi Burkhard Date: Tue, 18 Dec 2012 22:33:32 +0700 Subject: [PATCH] Fix #13075. Optimize $.type by preferring `typeof`. Close gh-1089. Also fixes browsers where `typeof RegExp === "function"`. --- src/core.js | 9 ++++++--- test/unit/core.js | 12 +++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 5927d1936..3d5f4be3f 100644 --- a/src/core.js +++ b/src/core.js @@ -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 ) { diff --git a/test/unit/core.js b/test/unit/core.js index 09f5c4c94..db21c41cc 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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() {