From 484cc6e22000aaa6cefab5752b165f6ebd62ffe1 Mon Sep 17 00:00:00 2001 From: Robert Katic Date: Sat, 28 Aug 2010 01:14:59 +0200 Subject: [PATCH] Made jQuery.type more consistent with host objects. --- src/core.js | 12 ++++++++++-- test/unit/core.js | 6 +++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/core.js b/src/core.js index 23d40bd0..158f4799 100644 --- a/src/core.js +++ b/src/core.js @@ -69,7 +69,10 @@ var jQuery = function( selector, context ) { push = Array.prototype.push, slice = Array.prototype.slice, trim = String.prototype.trim, - indexOf = Array.prototype.indexOf; + indexOf = Array.prototype.indexOf, + + // [[Class]] -> type pairs + class2type = {}; jQuery.fn = jQuery.prototype = { init: function( selector, context ) { @@ -487,7 +490,7 @@ jQuery.extend({ type: function( obj ) { return obj == null ? String( obj ) : - toString.call(obj).slice(8, -1).toLowerCase(); + class2type[ toString.call(obj) ] || "object"; }, isPlainObject: function( obj ) { @@ -799,6 +802,11 @@ jQuery.extend({ browser: {} }); +// Populate the class2type map +jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) { + class2type[ "[object " + name + "]" ] = name.toLowerCase(); +}); + browserMatch = jQuery.uaMatch( userAgent ); if ( browserMatch.browser ) { jQuery.browser[ browserMatch.browser ] = true; diff --git a/test/unit/core.js b/test/unit/core.js index d83f667d..85b8da64 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -219,7 +219,7 @@ test("trim", function() { }); test("type", function() { - expect(18); + expect(22); equals( jQuery.type(null), "null", "null" ); equals( jQuery.type(undefined), "undefined", "undefined" ); @@ -239,6 +239,10 @@ test("type", function() { equals( jQuery.type(new Date()), "date", "Date" ); equals( jQuery.type(new Function("return;")), "function", "Function" ); equals( jQuery.type(function(){}), "function", "Function" ); + equals( jQuery.type(window), "object", "Window" ); + equals( jQuery.type(document.body), "object", "Element" ); + equals( jQuery.type(document.createTextNode("foo")), "object", "TextNode" ); + equals( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" ); }); test("isPlainObject", function() {