深入学习jquery源码之isFunction()和isPlainObject()
isArray(obj)
概述
测试对象是否为数组。
参数
obj Object
用于测试是否为数组的对象
$("b").append( " + $.isArray([]) );
true
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
用于测试是否为窗口的对象
Is 'window' a window?
$("b").append( "" + $.isWindow(window) );
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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?