From ab551c2b14ac6b0511cf3da10ca224ce461a0f10 Mon Sep 17 00:00:00 2001 From: John Resig Date: Mon, 17 Nov 2008 16:32:05 +0000 Subject: [PATCH] Standardized the type checks across core. isFunction and isArray now use Object.prototype.toString to verify the type, .constructor use was removed in favor of typeof, typeof checks now use ===, undefined checks use === undefined. All of this is outlined in the new style guidelines: http://docs.jquery.com/JQuery_Core_Style_Guidelines#Type_Checks. Fixes bug #3618. --- src/ajax.js | 12 ++++----- src/core.js | 68 ++++++++++++++++++++++++------------------------- src/event.js | 8 +++--- src/fx.js | 8 +++--- src/offset.js | 2 +- src/selector.js | 8 +++--- 6 files changed, 52 insertions(+), 54 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 4135e34c4..55fc25d14 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -3,7 +3,7 @@ jQuery.fn.extend({ _load: jQuery.fn.load, load: function( url, params, callback ) { - if ( typeof url != 'string' ) + if ( typeof url !== "string" ) return this._load( url ); var off = url.indexOf(" "); @@ -24,7 +24,7 @@ jQuery.fn.extend({ params = null; // Otherwise, build a param string - } else if( typeof params == 'object' ) { + } else if( typeof params === "object" ) { params = jQuery.param( params ); type = "POST"; } @@ -178,7 +178,7 @@ jQuery.extend({ type = s.type.toUpperCase(); // convert data if not already a string - if ( s.data && s.processData && typeof s.data != "string" ) + if ( s.data && s.processData && typeof s.data !== "string" ) s.data = jQuery.param(s.data); // Handle JSONP Parameter Callbacks @@ -459,7 +459,7 @@ jQuery.extend({ // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 return !xhr.status && location.protocol == "file:" || ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || - jQuery.browser.safari && xhr.status == undefined; + jQuery.browser.safari && xhr.status === undefined; } catch(e){} return false; }, @@ -471,7 +471,7 @@ jQuery.extend({ // Firefox always returns 200. check Last-Modified date return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || - jQuery.browser.safari && xhr.status == undefined; + jQuery.browser.safari && xhr.status === undefined; } catch(e){} return false; }, @@ -490,7 +490,7 @@ jQuery.extend({ data = s.dataFilter( data, type ); // The filter can actually parse the response - if( typeof data == 'string' ){ + if( typeof data === "string" ){ // If the type is "script", eval it in global context if ( type == "script" ) diff --git a/src/core.js b/src/core.js index 081c5b439..b731eb839 100644 --- a/src/core.js +++ b/src/core.js @@ -41,7 +41,7 @@ jQuery.fn = jQuery.prototype = { return this; } // Handle HTML strings - if ( typeof selector == "string" ) { + if ( typeof selector === "string" ) { // Are we dealing with HTML string or an ID? var match = quickExpr.exec( selector ); @@ -93,7 +93,7 @@ jQuery.fn = jQuery.prototype = { // Get the Nth element in the matched element set OR // Get the whole matched element set as a clean array get: function( num ) { - return num == undefined ? + return num === undefined ? // Return a 'clean' array jQuery.makeArray( this ) : @@ -150,7 +150,7 @@ jQuery.fn = jQuery.prototype = { var options = name; // Look for the case where we're accessing a style value - if ( name.constructor == String ) + if ( typeof name === "string" ) if ( value === undefined ) return this[0] && jQuery[ type || "attr" ]( this[0], name ); @@ -180,7 +180,7 @@ jQuery.fn = jQuery.prototype = { }, text: function( text ) { - if ( typeof text != "object" && text != null ) + if ( typeof text !== "object" && text != null ) return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) ); var ret = ""; @@ -292,7 +292,7 @@ jQuery.fn = jQuery.prototype = { // removeData doesn't work here, IE removes it from the original as well // this is primarily for IE but the data expando shouldn't be copied over in any browser var clone = ret.find("*").andSelf().each(function(){ - if ( this[ expando ] != undefined ) + if ( this[ expando ] !== undefined ) this[ expando ] = null; }); @@ -323,7 +323,7 @@ jQuery.fn = jQuery.prototype = { }, not: function( selector ) { - if ( selector.constructor == String ) + if ( typeof selector === "string" ) // test special case where just one selector is passed in if ( isSimple.test( selector ) ) return this.pushStack( jQuery.multiFilter( selector, this, true ) ); @@ -339,7 +339,7 @@ jQuery.fn = jQuery.prototype = { add: function( selector ) { return this.pushStack( jQuery.unique( jQuery.merge( this.get(), - typeof selector == 'string' ? + typeof selector === "string" ? jQuery( selector ) : jQuery.makeArray( selector ) ))); @@ -354,7 +354,7 @@ jQuery.fn = jQuery.prototype = { }, val: function( value ) { - if ( value == undefined ) { + if ( value === undefined ) { var elem = this[0]; if ( elem ) { @@ -400,7 +400,7 @@ jQuery.fn = jQuery.prototype = { return undefined; } - if( value.constructor == Number ) + if ( typeof value === "number" ) value += ''; return this.each(function(){ @@ -428,7 +428,7 @@ jQuery.fn = jQuery.prototype = { }, html: function( value ) { - return value == undefined ? + return value === undefined ? (this[0] ? this[0].innerHTML : null) : @@ -550,7 +550,7 @@ jQuery.extend = jQuery.fn.extend = function() { var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options; // Handle a deep copy situation - if ( target.constructor == Boolean ) { + if ( typeof target === "boolean" ) { deep = target; target = arguments[1] || {}; // skip the boolean and the target @@ -558,7 +558,7 @@ jQuery.extend = jQuery.fn.extend = function() { } // Handle case when target is a string or something (possible in deep copy) - if ( typeof target != "object" && typeof target != "function" ) + if ( typeof target !== "object" && !jQuery.isFunction(target) ) target = {}; // extend jQuery itself if only one argument is passed @@ -579,7 +579,7 @@ jQuery.extend = jQuery.fn.extend = function() { continue; // Recurse if we're merging object values - if ( deep && copy && typeof copy == "object" && !copy.nodeType ) + if ( deep && copy && typeof copy === "object" && !copy.nodeType ) target[ name ] = jQuery.extend( deep, // Never move original objects, clone them src || ( copy.length != null ? [ ] : { } ) @@ -599,7 +599,8 @@ var expando = "jQuery" + now(), uuid = 0, windowData = {}, // exclude the following css properties to add px exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, // cache defaultView - defaultView = document.defaultView || {}; + defaultView = document.defaultView || {}, + toString = Object.prototype.toString; jQuery.extend({ noConflict: function( deep ) { @@ -611,19 +612,15 @@ jQuery.extend({ return jQuery; }, - // See test/unit/core.js for details concerning this function. + // See test/unit/core.js for details concerning isFunction. // Since version 1.3, DOM methods and functions like alert // aren't supported. They return false on IE (#2968). - - // Memory leaks appear in IE6 when applying instanceof - // to the window, document or any other COM object (#3485) - // http://ajaxian.com/archives/working-aroung-the-instanceof-memory-leak - isFunction: function( fn ) { - return !!fn && !!fn.hasOwnProperty && fn instanceof Function; + isFunction: function( obj ) { + return toString.call(obj) === "[object Function]"; }, - - isArray: function( arr ){ - return !!arr && arr.constructor == Array; + + isArray: function( obj ) { + return toString.call(obj) === "[object Array]"; }, // check if an element is in a (or is an) XML document @@ -732,7 +729,7 @@ jQuery.extend({ var name, i = 0, length = object.length; if ( args ) { - if ( length == undefined ) { + if ( length === undefined ) { for ( name in object ) if ( callback.apply( object[ name ], args ) === false ) break; @@ -743,7 +740,7 @@ jQuery.extend({ // A special, fast, case for the most common use of each } else { - if ( length == undefined ) { + if ( length === undefined ) { for ( name in object ) if ( callback.call( object[ name ], name, object[ name ] ) === false ) break; @@ -761,7 +758,7 @@ jQuery.extend({ value = value.call( elem, i ); // Handle passing in a number to a CSS property - return value && value.constructor == Number && type == "curCSS" && !exclude.test( name ) ? + return typeof value === "number" && type == "curCSS" && !exclude.test( name ) ? value + "px" : value; }, @@ -778,7 +775,7 @@ jQuery.extend({ // internal only, use removeClass("class") remove: function( elem, classNames ) { if (elem.nodeType == 1) - elem.className = classNames != undefined ? + elem.className = classNames !== undefined ? jQuery.grep(elem.className.split(/\s+/), function(className){ return !jQuery.className.has( classNames, className ); }).join(" ") : @@ -946,19 +943,20 @@ jQuery.extend({ clean: function( elems, context ) { var ret = []; context = context || document; + // !context.createElement fails in IE with an error but returns typeof 'object' - if (typeof context.createElement == 'undefined') + if ( context.createElement === undefined ) context = context.ownerDocument || context[0] && context[0].ownerDocument || document; jQuery.each(elems, function(i, elem){ - if ( typeof elem == 'number' ) + if ( typeof elem === "number" ) elem += ''; if ( !elem ) return; // Convert html string into DOM nodes - if ( typeof elem == "string" ) { + if ( typeof elem === "string" ) { // Fix "XHTML"-style tags in all browsers elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){ return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? @@ -1031,7 +1029,7 @@ jQuery.extend({ if ( elem.length === 0 && (!jQuery.nodeName( elem, "form" ) && !jQuery.nodeName( elem, "select" )) ) return; - if ( elem[0] == undefined || jQuery.nodeName( elem, "form" ) || elem.options ) + if ( elem[0] === undefined || jQuery.nodeName( elem, "form" ) || elem.options ) ret.push( elem ); else @@ -1139,7 +1137,7 @@ jQuery.extend({ if( array != null ){ var i = array.length; // The window, strings (and functions) also have 'length' - if( i == null || typeof array == 'string' || jQuery.isFunction(array) || array.setInterval ) + if( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) ret[0] = array; else while( i ) @@ -1364,12 +1362,12 @@ jQuery.each([ "Height", "Width" ], function(i, name){ ) : // Get or set width or height on the element - size == undefined ? + size === undefined ? // Get width or height on the element (this.length ? jQuery.css( this[0], type ) : null) : // Set the width or height on the element (default to pixels if value is unitless) - this.css( type, size.constructor == String ? size : size + "px" ); + this.css( type, typeof size === "string" ? size : size + "px" ); }; }); diff --git a/src/event.js b/src/event.js index f082e7eff..4bf96fca1 100644 --- a/src/event.js +++ b/src/event.js @@ -21,7 +21,7 @@ jQuery.event = { handler.guid = this.guid++; // if data is passed, bind to handler - if( data != undefined ) { + if ( data !== undefined ) { // Create temporary function pointer to original handler var fn = handler; @@ -40,7 +40,7 @@ jQuery.event = { handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){ // Handle the second event of a trigger and when // an event is called after a page has unloaded - if ( typeof jQuery != "undefined" && !jQuery.event.triggered ) + if ( typeof jQuery !== "undefined" && !jQuery.event.triggered ) return jQuery.event.handle.apply(arguments.callee.elem, arguments); }); // Add elem as a property of the handle function @@ -99,7 +99,7 @@ jQuery.event = { if ( events ) { // Unbind all events for the element - if ( types == undefined || (typeof types == "string" && types.charAt(0) == ".") ) + if ( types === undefined || (typeof types === "string" && types.charAt(0) == ".") ) for ( var type in events ) this.remove( elem, type + (types || "") ); else { @@ -219,7 +219,7 @@ jQuery.event = { // call the extra function and tack the current return value on the end for possible inspection ret = extra.apply( elem, val == null ? data : data.concat( val ) ); // if anything is returned, give it precedence and have it overwrite the previous value - if (ret !== undefined) + if ( ret !== undefined ) val = ret; } diff --git a/src/fx.js b/src/fx.js index cbf067f37..bc14d3231 100644 --- a/src/fx.js +++ b/src/fx.js @@ -117,7 +117,7 @@ jQuery.fn.extend({ type = "fx"; } - if ( !type || (typeof type == "string" && !fn) ) + if ( !type || (typeof type === "string" && !fn) ) return queue( this[0], type ); return this.each(function(){ @@ -201,14 +201,14 @@ jQuery.fn.dequeue = function(type){ jQuery.extend({ speed: function(speed, easing, fn) { - var opt = speed && speed.constructor == Object ? speed : { + var opt = typeof speed === "object" ? speed : { complete: fn || !fn && easing || jQuery.isFunction( speed ) && speed, duration: speed, - easing: fn && easing || easing && easing.constructor != Function && easing + easing: fn && easing || jQuery.isFunction(easing) && easing }; - opt.duration = jQuery.fx.off ? 0 : typeof opt.duration == 'number' ? opt.duration : + opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration : jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default; // Queueing diff --git a/src/offset.js b/src/offset.js index 5c600cc2e..30dedbc84 100644 --- a/src/offset.js +++ b/src/offset.js @@ -133,7 +133,7 @@ jQuery.each( ['Left', 'Top'], function(i, name) { jQuery.fn[ method ] = function(val) { if (!this[0]) return; - return val != undefined ? + return val !== undefined ? // Set the scroll offset this.each(function() { diff --git a/src/selector.js b/src/selector.js index 6dcab37fe..607fa619a 100644 --- a/src/selector.js +++ b/src/selector.js @@ -92,7 +92,7 @@ jQuery.extend({ find: function( t, context ) { // Quickly handle non-string expressions - if ( typeof t != "string" ) + if ( typeof t !== "string" ) return [ t ]; // check to make sure context is a DOM element or a document @@ -214,7 +214,7 @@ jQuery.extend({ // Do a quick check for the existence of the actual ID attribute // to avoid selecting by the name attribute in IE // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form - if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && oid.id != m[2] ) + if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id === "string" && oid.id != m[2] ) oid = jQuery('[@id="'+m[2]+'"]', elem)[0]; // Do a quick check for node name (where applicable) so @@ -391,10 +391,10 @@ jQuery.extend({ // Otherwise, find the expression to execute } else { var fn = jQuery.expr[ m[1] ]; - if ( typeof fn == "object" ) + if ( typeof fn === "object" ) fn = fn[ m[2] ]; - if ( typeof fn == "string" ) + if ( typeof fn === "string" ) fn = eval("false||function(a,i){return " + fn + ";}"); // Execute it against the current filter