Skip to content

Commit 5eec75e

Browse files
hasclassdmethvin
authored andcommitted
Fix #13075. Optimize $.type by preferring typeof. Close jquerygh-1089.
Also fixes browsers where `typeof RegExp === "function"`.
1 parent d829804 commit 5eec75e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/core.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,12 @@ jQuery.extend({
427427
},
428428

429429
type: function( obj ) {
430-
return obj == null ?
431-
String( obj ) :
432-
class2type[ core_toString.call(obj) ] || "object";
430+
if ( obj == null ) {
431+
return String( obj );
432+
}
433+
return typeof obj === "object" || typeof obj === "function" ?
434+
class2type[ core_toString.call(obj) ] || "object" :
435+
typeof obj;
433436
},
434437

435438
isPlainObject: function( obj ) {

test/unit/core.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ test("trim", function() {
243243
});
244244

245245
test("type", function() {
246-
expect( 24 );
246+
expect( 28 );
247247

248248
equal( jQuery.type(null), "null", "null" );
249249
equal( jQuery.type(undefined), "undefined", "undefined" );
@@ -269,6 +269,16 @@ test("type", function() {
269269
equal( jQuery.type(document.body), "object", "Element" );
270270
equal( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
271271
equal( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
272+
273+
// Avoid Lint complaints
274+
var MyString = String;
275+
var MyNumber = Number;
276+
var MyBoolean = Boolean;
277+
var MyObject = Object;
278+
equal( jQuery.type(new MyBoolean(true)), "boolean", "Boolean" );
279+
equal( jQuery.type(new MyNumber(1)), "number", "Number" );
280+
equal( jQuery.type(new MyString("a")), "string", "String" );
281+
equal( jQuery.type(new MyObject()), "object", "Object" );
272282
});
273283

274284
asyncTest("isPlainObject", function() {

0 commit comments

Comments
 (0)