深入学习jquery源码之isFunction()和isPlainObject()
isArray(obj)
概述
测试对象是否为数组。
参数
obj Object
用于测试是否为数组的对象
$("b").append( " + $.isArray([]) );
<b>true</b>
isFunction(obj)
概述
测试对象是否为函数。
注意:jQuery 1.3以后,在IE浏览器里,浏览器提供的函数比如'alert'还有 DOM 元素的方法比如 'getAttribute' 将不认为是函数
参数
obj Object
用于测试是否为函数的对象
检测是否为函数
function stub() {
}
var objs = [
function () {},
{ x:15, y:20 },
null,
stub,
"function"
];
jQuery.each(objs, function (i) {
var isFunc = jQuery.isFunction(objs[i]);
$("span:eq( " + i + ")").text(isFunc);
});
[ true,false,false,true,false ]
isEmptyObject(obj)
概述
jQuery 1.4 新增。测试对象是否是空对象(不包含任何属性)。
jQuery 1.4 中,这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
参数
obj Object
用于测试是否为空对象
jQuery.isEmptyObject({}) // true
jQuery.isEmptyObject({ foo: "bar" }) // false
isPlainObject(obj)
概述
测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
obj Object
用于测试是否为纯粹的对象
测试是否为纯粹的对象
jQuery.isPlainObject({}) // true
jQuery.isPlainObject("test") // false
isWindow(obj)
概述
测试对象是否是窗口(有可能是Frame)。
参数
obj Object
用于测试是否为窗口的对象
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.2.js"></script>
</head>
<body>
Is 'window' a window? <b></b>
<script>$("b").append( "" + $.isWindow(window) );</script>
</body>
</html>
isNumeric(value)
概述
确定它的参数是否是一个数字。
$.isNumeric() 方法检查它的参数是否代表一个数值。如果是这样,它返回 true。否则,它返回false。该参数可以是任何类型的
参数
value
用于测试的值。
$.isNumeric("-10"); // true
$.isNumeric(16); // true
$.isNumeric(0xFF); // true
$.isNumeric("0xFF"); // true
$.isNumeric("8e5"); // true (exponential notation string)
$.isNumeric(3.1415); // true
$.isNumeric(+10); // true
$.isNumeric(0144); // true (octal integer literal)
$.isNumeric(""); // false
$.isNumeric({}); // false (empty object)
$.isNumeric(NaN); // false
$.isNumeric(null); // false
$.isNumeric(true); // false
$.isNumeric(Infinity); // false
$.isNumeric(undefined); // false
type(obj)
概述
检测obj的数据类型。
参数
obj Object
用于测试类型的对象
jQuery.type(true) === "boolean"
jQuery.type(3) === "number"
jQuery.type("test") === "string"
jQuery.type(function(){}) === "function"
jQuery.type([]) === "array"
jQuery.type(new Date()) === "date"
jQuery.type(/test/) === "regexp"
jquery源码
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var support = {};
jQuery.extend({
// Unique for each copy of jQuery on the page
expando: "jQuery" + (version + Math.random()).replace(/\D/g, ""),
// Assume jQuery is ready without the ready module
isReady: true,
error: function (msg) {
throw new Error(msg);
},
noop: 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).
isFunction: function (obj) {
return jQuery.type(obj) === "function";
},
isArray: Array.isArray || function (obj) {
return jQuery.type(obj) === "array";
},
isWindow: function (obj) {
/* jshint eqeqeq: false */
return obj != null && obj == obj.window;
},
isNumeric: function (obj) {
// parseFloat NaNs numeric-cast false positives (null|true|false|"")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
// adding 1 corrects loss of precision from parseFloat (#15100)
return !jQuery.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;
},
isEmptyObject: function (obj) {
var name;
for (name in obj) {
return false;
}
return true;
},
isPlainObject: function (obj) {
var key;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow(obj)) {
return false;
}
try {
// Not own constructor property must be Object
if (obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
return false;
}
} catch (e) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Support: IE<9
// Handle iteration over inherited properties before own properties.
if (support.ownLast) {
for (key in obj) {
return hasOwn.call(obj, key);
}
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for (key in obj) { }
return key === undefined || hasOwn.call(obj, key);
},
type: function (obj) {
if (obj == null) {
return obj + "";
}
return typeof obj === "object" || typeof obj === "function" ?
class2type[toString.call(obj)] || "object" :
typeof obj;
}
// jQuery.support is not used in Core but other projects attach their
// properties to it so it needs to exist.
support: support
});
// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function (i, name) {
class2type["[object " + name + "]"] = name.toLowerCase();
});