jQuery工具方法中的判断方法

对象自变量:

两种形式:|obj| is a plain object, created by {} or constructed with new Object

1. var obj = {};

2. var obj = new Object();

obj.constructor.prototype中有isPrototypeof ()方法,因为只有Object.prototype中有isPrototypeof ()方法。

isPlainObject: function( obj ) {
		// Not plain objects:
		// - Any object or value whose internal [[Class]] property is not "[object Object]"
		// - DOM nodes
		// - window
		if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
			return false;
		}

		// Support: Firefox <20
		// The try/catch suppresses exceptions thrown when attempting to access
		// the "constructor" property of certain host objects, ie. |window.location|
		// https://bugzilla.mozilla.org/show_bug.cgi?id=814622
		try {
			if ( obj.constructor &&
					!core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
				return false;
			}
		} catch ( e ) {
			return false;
		}

		// If the function hasn't returned already, we're confident that
		// |obj| is a plain object, created by {} or constructed with new Object
		return true;
	},
var arr = [],
    obj1 = {},
    obj2 = new Object();
console.log($.isPlainObject(arr));//false
console.log($.isPlainObject(obj1));//true
console.log($.isPlainObject(obj2));//true

window的判断:

isWindow: function( obj ) {
		return obj != null && obj === obj.window;
	},

null 和undefined不能添加属性,否则会报错
var obj = null;
obj.a=100;//Uncaught TypeError: Cannot set property 'a' of null
var obj = undefined;
obj.a=100;//Uncaught TypeError: Cannot set property 'a' of undefined
window既可以是全局对象,也可是是全局对象下的窗口对象
console.log(window === window.window);//true


其他判断:

isFunction: function( obj ) {
		return jQuery.type(obj) === "function";
	},

isArray: Array.isArray,

isNumeric: function( obj ) {
	        return !isNaN( parseFloat(obj) ) && isFinite( obj );
	},
isEmptyObject: function( obj ) {
		var name;
		for ( name in obj ) {
			return false;
		}
		return true;
	},




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值