Commit ab551c2b authored by John Resig's avatar John Resig

Standardized the type checks across core. isFunction and isArray now use...

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.
parent 0aa8d40c
...@@ -3,7 +3,7 @@ jQuery.fn.extend({ ...@@ -3,7 +3,7 @@ jQuery.fn.extend({
_load: jQuery.fn.load, _load: jQuery.fn.load,
load: function( url, params, callback ) { load: function( url, params, callback ) {
if ( typeof url != 'string' ) if ( typeof url !== "string" )
return this._load( url ); return this._load( url );
var off = url.indexOf(" "); var off = url.indexOf(" ");
...@@ -24,7 +24,7 @@ jQuery.fn.extend({ ...@@ -24,7 +24,7 @@ jQuery.fn.extend({
params = null; params = null;
// Otherwise, build a param string // Otherwise, build a param string
} else if( typeof params == 'object' ) { } else if( typeof params === "object" ) {
params = jQuery.param( params ); params = jQuery.param( params );
type = "POST"; type = "POST";
} }
...@@ -178,7 +178,7 @@ jQuery.extend({ ...@@ -178,7 +178,7 @@ jQuery.extend({
type = s.type.toUpperCase(); type = s.type.toUpperCase();
// convert data if not already a string // 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); s.data = jQuery.param(s.data);
// Handle JSONP Parameter Callbacks // Handle JSONP Parameter Callbacks
...@@ -459,7 +459,7 @@ jQuery.extend({ ...@@ -459,7 +459,7 @@ jQuery.extend({
// IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
return !xhr.status && location.protocol == "file:" || return !xhr.status && location.protocol == "file:" ||
( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || ( 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){} } catch(e){}
return false; return false;
}, },
...@@ -471,7 +471,7 @@ jQuery.extend({ ...@@ -471,7 +471,7 @@ jQuery.extend({
// Firefox always returns 200. check Last-Modified date // Firefox always returns 200. check Last-Modified date
return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || return xhr.status == 304 || xhrRes == jQuery.lastModified[url] ||
jQuery.browser.safari && xhr.status == undefined; jQuery.browser.safari && xhr.status === undefined;
} catch(e){} } catch(e){}
return false; return false;
}, },
...@@ -490,7 +490,7 @@ jQuery.extend({ ...@@ -490,7 +490,7 @@ jQuery.extend({
data = s.dataFilter( data, type ); data = s.dataFilter( data, type );
// The filter can actually parse the response // 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 the type is "script", eval it in global context
if ( type == "script" ) if ( type == "script" )
......
...@@ -41,7 +41,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -41,7 +41,7 @@ jQuery.fn = jQuery.prototype = {
return this; return this;
} }
// Handle HTML strings // Handle HTML strings
if ( typeof selector == "string" ) { if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID? // Are we dealing with HTML string or an ID?
var match = quickExpr.exec( selector ); var match = quickExpr.exec( selector );
...@@ -93,7 +93,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -93,7 +93,7 @@ jQuery.fn = jQuery.prototype = {
// Get the Nth element in the matched element set OR // Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array // Get the whole matched element set as a clean array
get: function( num ) { get: function( num ) {
return num == undefined ? return num === undefined ?
// Return a 'clean' array // Return a 'clean' array
jQuery.makeArray( this ) : jQuery.makeArray( this ) :
...@@ -150,7 +150,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -150,7 +150,7 @@ jQuery.fn = jQuery.prototype = {
var options = name; var options = name;
// Look for the case where we're accessing a style value // Look for the case where we're accessing a style value
if ( name.constructor == String ) if ( typeof name === "string" )
if ( value === undefined ) if ( value === undefined )
return this[0] && jQuery[ type || "attr" ]( this[0], name ); return this[0] && jQuery[ type || "attr" ]( this[0], name );
...@@ -180,7 +180,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -180,7 +180,7 @@ jQuery.fn = jQuery.prototype = {
}, },
text: function( text ) { 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 ) ); return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
var ret = ""; var ret = "";
...@@ -292,7 +292,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -292,7 +292,7 @@ jQuery.fn = jQuery.prototype = {
// removeData doesn't work here, IE removes it from the original as well // 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 // this is primarily for IE but the data expando shouldn't be copied over in any browser
var clone = ret.find("*").andSelf().each(function(){ var clone = ret.find("*").andSelf().each(function(){
if ( this[ expando ] != undefined ) if ( this[ expando ] !== undefined )
this[ expando ] = null; this[ expando ] = null;
}); });
...@@ -323,7 +323,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -323,7 +323,7 @@ jQuery.fn = jQuery.prototype = {
}, },
not: function( selector ) { not: function( selector ) {
if ( selector.constructor == String ) if ( typeof selector === "string" )
// test special case where just one selector is passed in // test special case where just one selector is passed in
if ( isSimple.test( selector ) ) if ( isSimple.test( selector ) )
return this.pushStack( jQuery.multiFilter( selector, this, true ) ); return this.pushStack( jQuery.multiFilter( selector, this, true ) );
...@@ -339,7 +339,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -339,7 +339,7 @@ jQuery.fn = jQuery.prototype = {
add: function( selector ) { add: function( selector ) {
return this.pushStack( jQuery.unique( jQuery.merge( return this.pushStack( jQuery.unique( jQuery.merge(
this.get(), this.get(),
typeof selector == 'string' ? typeof selector === "string" ?
jQuery( selector ) : jQuery( selector ) :
jQuery.makeArray( selector ) jQuery.makeArray( selector )
))); )));
...@@ -354,7 +354,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -354,7 +354,7 @@ jQuery.fn = jQuery.prototype = {
}, },
val: function( value ) { val: function( value ) {
if ( value == undefined ) { if ( value === undefined ) {
var elem = this[0]; var elem = this[0];
if ( elem ) { if ( elem ) {
...@@ -400,7 +400,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -400,7 +400,7 @@ jQuery.fn = jQuery.prototype = {
return undefined; return undefined;
} }
if( value.constructor == Number ) if ( typeof value === "number" )
value += ''; value += '';
return this.each(function(){ return this.each(function(){
...@@ -428,7 +428,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -428,7 +428,7 @@ jQuery.fn = jQuery.prototype = {
}, },
html: function( value ) { html: function( value ) {
return value == undefined ? return value === undefined ?
(this[0] ? (this[0] ?
this[0].innerHTML : this[0].innerHTML :
null) : null) :
...@@ -550,7 +550,7 @@ jQuery.extend = jQuery.fn.extend = function() { ...@@ -550,7 +550,7 @@ jQuery.extend = jQuery.fn.extend = function() {
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options; var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
// Handle a deep copy situation // Handle a deep copy situation
if ( target.constructor == Boolean ) { if ( typeof target === "boolean" ) {
deep = target; deep = target;
target = arguments[1] || {}; target = arguments[1] || {};
// skip the boolean and the target // skip the boolean and the target
...@@ -558,7 +558,7 @@ jQuery.extend = jQuery.fn.extend = function() { ...@@ -558,7 +558,7 @@ jQuery.extend = jQuery.fn.extend = function() {
} }
// Handle case when target is a string or something (possible in deep copy) // 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 = {}; target = {};
// extend jQuery itself if only one argument is passed // extend jQuery itself if only one argument is passed
...@@ -579,7 +579,7 @@ jQuery.extend = jQuery.fn.extend = function() { ...@@ -579,7 +579,7 @@ jQuery.extend = jQuery.fn.extend = function() {
continue; continue;
// Recurse if we're merging object values // 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, target[ name ] = jQuery.extend( deep,
// Never move original objects, clone them // Never move original objects, clone them
src || ( copy.length != null ? [ ] : { } ) src || ( copy.length != null ? [ ] : { } )
...@@ -599,7 +599,8 @@ var expando = "jQuery" + now(), uuid = 0, windowData = {}, ...@@ -599,7 +599,8 @@ var expando = "jQuery" + now(), uuid = 0, windowData = {},
// exclude the following css properties to add px // exclude the following css properties to add px
exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
// cache defaultView // cache defaultView
defaultView = document.defaultView || {}; defaultView = document.defaultView || {},
toString = Object.prototype.toString;
jQuery.extend({ jQuery.extend({
noConflict: function( deep ) { noConflict: function( deep ) {
...@@ -611,19 +612,15 @@ jQuery.extend({ ...@@ -611,19 +612,15 @@ jQuery.extend({
return jQuery; 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 // Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968). // aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
// Memory leaks appear in IE6 when applying instanceof return toString.call(obj) === "[object Function]";
// 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;
}, },
isArray: function( arr ){ isArray: function( obj ) {
return !!arr && arr.constructor == Array; return toString.call(obj) === "[object Array]";
}, },
// check if an element is in a (or is an) XML document // check if an element is in a (or is an) XML document
...@@ -732,7 +729,7 @@ jQuery.extend({ ...@@ -732,7 +729,7 @@ jQuery.extend({
var name, i = 0, length = object.length; var name, i = 0, length = object.length;
if ( args ) { if ( args ) {
if ( length == undefined ) { if ( length === undefined ) {
for ( name in object ) for ( name in object )
if ( callback.apply( object[ name ], args ) === false ) if ( callback.apply( object[ name ], args ) === false )
break; break;
...@@ -743,7 +740,7 @@ jQuery.extend({ ...@@ -743,7 +740,7 @@ jQuery.extend({
// A special, fast, case for the most common use of each // A special, fast, case for the most common use of each
} else { } else {
if ( length == undefined ) { if ( length === undefined ) {
for ( name in object ) for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) === false ) if ( callback.call( object[ name ], name, object[ name ] ) === false )
break; break;
...@@ -761,7 +758,7 @@ jQuery.extend({ ...@@ -761,7 +758,7 @@ jQuery.extend({
value = value.call( elem, i ); value = value.call( elem, i );
// Handle passing in a number to a CSS property // 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 + "px" :
value; value;
}, },
...@@ -778,7 +775,7 @@ jQuery.extend({ ...@@ -778,7 +775,7 @@ jQuery.extend({
// internal only, use removeClass("class") // internal only, use removeClass("class")
remove: function( elem, classNames ) { remove: function( elem, classNames ) {
if (elem.nodeType == 1) if (elem.nodeType == 1)
elem.className = classNames != undefined ? elem.className = classNames !== undefined ?
jQuery.grep(elem.className.split(/\s+/), function(className){ jQuery.grep(elem.className.split(/\s+/), function(className){
return !jQuery.className.has( classNames, className ); return !jQuery.className.has( classNames, className );
}).join(" ") : }).join(" ") :
...@@ -946,19 +943,20 @@ jQuery.extend({ ...@@ -946,19 +943,20 @@ jQuery.extend({
clean: function( elems, context ) { clean: function( elems, context ) {
var ret = []; var ret = [];
context = context || document; context = context || document;
// !context.createElement fails in IE with an error but returns typeof 'object' // !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; context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
jQuery.each(elems, function(i, elem){ jQuery.each(elems, function(i, elem){
if ( typeof elem == 'number' ) if ( typeof elem === "number" )
elem += ''; elem += '';
if ( !elem ) if ( !elem )
return; return;
// Convert html string into DOM nodes // Convert html string into DOM nodes
if ( typeof elem == "string" ) { if ( typeof elem === "string" ) {
// Fix "XHTML"-style tags in all browsers // Fix "XHTML"-style tags in all browsers
elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){ elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){
return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ?
...@@ -1031,7 +1029,7 @@ jQuery.extend({ ...@@ -1031,7 +1029,7 @@ jQuery.extend({
if ( elem.length === 0 && (!jQuery.nodeName( elem, "form" ) && !jQuery.nodeName( elem, "select" )) ) if ( elem.length === 0 && (!jQuery.nodeName( elem, "form" ) && !jQuery.nodeName( elem, "select" )) )
return; return;
if ( elem[0] == undefined || jQuery.nodeName( elem, "form" ) || elem.options ) if ( elem[0] === undefined || jQuery.nodeName( elem, "form" ) || elem.options )
ret.push( elem ); ret.push( elem );
else else
...@@ -1139,7 +1137,7 @@ jQuery.extend({ ...@@ -1139,7 +1137,7 @@ jQuery.extend({
if( array != null ){ if( array != null ){
var i = array.length; var i = array.length;
// The window, strings (and functions) also have '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; ret[0] = array;
else else
while( i ) while( i )
...@@ -1364,12 +1362,12 @@ jQuery.each([ "Height", "Width" ], function(i, name){ ...@@ -1364,12 +1362,12 @@ jQuery.each([ "Height", "Width" ], function(i, name){
) : ) :
// Get or set width or height on the element // Get or set width or height on the element
size == undefined ? size === undefined ?
// Get width or height on the element // Get width or height on the element
(this.length ? jQuery.css( this[0], type ) : null) : (this.length ? jQuery.css( this[0], type ) : null) :
// Set the width or height on the element (default to pixels if value is unitless) // 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" );
}; };
}); });
......
...@@ -21,7 +21,7 @@ jQuery.event = { ...@@ -21,7 +21,7 @@ jQuery.event = {
handler.guid = this.guid++; handler.guid = this.guid++;
// if data is passed, bind to handler // if data is passed, bind to handler
if( data != undefined ) { if ( data !== undefined ) {
// Create temporary function pointer to original handler // Create temporary function pointer to original handler
var fn = handler; var fn = handler;
...@@ -40,7 +40,7 @@ jQuery.event = { ...@@ -40,7 +40,7 @@ jQuery.event = {
handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){ handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
// Handle the second event of a trigger and when // Handle the second event of a trigger and when
// an event is called after a page has unloaded // 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); return jQuery.event.handle.apply(arguments.callee.elem, arguments);
}); });
// Add elem as a property of the handle function // Add elem as a property of the handle function
...@@ -99,7 +99,7 @@ jQuery.event = { ...@@ -99,7 +99,7 @@ jQuery.event = {
if ( events ) { if ( events ) {
// Unbind all events for the element // 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 ) for ( var type in events )
this.remove( elem, type + (types || "") ); this.remove( elem, type + (types || "") );
else { else {
...@@ -219,7 +219,7 @@ jQuery.event = { ...@@ -219,7 +219,7 @@ jQuery.event = {
// call the extra function and tack the current return value on the end for possible inspection // 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 ) ); 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 anything is returned, give it precedence and have it overwrite the previous value
if (ret !== undefined) if ( ret !== undefined )
val = ret; val = ret;
} }
......
...@@ -117,7 +117,7 @@ jQuery.fn.extend({ ...@@ -117,7 +117,7 @@ jQuery.fn.extend({
type = "fx"; type = "fx";
} }
if ( !type || (typeof type == "string" && !fn) ) if ( !type || (typeof type === "string" && !fn) )
return queue( this[0], type ); return queue( this[0], type );
return this.each(function(){ return this.each(function(){
...@@ -201,14 +201,14 @@ jQuery.fn.dequeue = function(type){ ...@@ -201,14 +201,14 @@ jQuery.fn.dequeue = function(type){
jQuery.extend({ jQuery.extend({
speed: function(speed, easing, fn) { speed: function(speed, easing, fn) {
var opt = speed && speed.constructor == Object ? speed : { var opt = typeof speed === "object" ? speed : {
complete: fn || !fn && easing || complete: fn || !fn && easing ||
jQuery.isFunction( speed ) && speed, jQuery.isFunction( speed ) && speed,
duration: 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; jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
// Queueing // Queueing
......
...@@ -133,7 +133,7 @@ jQuery.each( ['Left', 'Top'], function(i, name) { ...@@ -133,7 +133,7 @@ jQuery.each( ['Left', 'Top'], function(i, name) {
jQuery.fn[ method ] = function(val) { jQuery.fn[ method ] = function(val) {
if (!this[0]) return; if (!this[0]) return;
return val != undefined ? return val !== undefined ?
// Set the scroll offset // Set the scroll offset
this.each(function() { this.each(function() {
......
...@@ -92,7 +92,7 @@ jQuery.extend({ ...@@ -92,7 +92,7 @@ jQuery.extend({
find: function( t, context ) { find: function( t, context ) {
// Quickly handle non-string expressions // Quickly handle non-string expressions
if ( typeof t != "string" ) if ( typeof t !== "string" )
return [ t ]; return [ t ];
// check to make sure context is a DOM element or a document // check to make sure context is a DOM element or a document
...@@ -214,7 +214,7 @@ jQuery.extend({ ...@@ -214,7 +214,7 @@ jQuery.extend({
// Do a quick check for the existence of the actual ID attribute // Do a quick check for the existence of the actual ID attribute
// to avoid selecting by the name attribute in IE // 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 // 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]; oid = jQuery('[@id="'+m[2]+'"]', elem)[0];
// Do a quick check for node name (where applicable) so // Do a quick check for node name (where applicable) so
...@@ -391,10 +391,10 @@ jQuery.extend({ ...@@ -391,10 +391,10 @@ jQuery.extend({
// Otherwise, find the expression to execute // Otherwise, find the expression to execute
} else { } else {
var fn = jQuery.expr[ m[1] ]; var fn = jQuery.expr[ m[1] ];
if ( typeof fn == "object" ) if ( typeof fn === "object" )
fn = fn[ m[2] ]; fn = fn[ m[2] ];
if ( typeof fn == "string" ) if ( typeof fn === "string" )
fn = eval("false||function(a,i){return " + fn + ";}"); fn = eval("false||function(a,i){return " + fn + ";}");
// Execute it against the current filter // Execute it against the current filter
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment