jQuery源码分析6: jQuery.isEmptyObject与jQuery.isPlainObject

jQuery源码分析6: jQuery.isEmptyObject与jQuery.isPlainObject

var hasOwn = Object.prototype.hasOwnProperty,

isEmptyObject: function( obj ) {
for ( var name in obj ) {
return false;
}
return true;
},

isPlainObject: function( obj ) {
// 必须是一个Object,同时需要过滤掉DOM对象
// Because of IE, we also have to check the presence of the constructor property.
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}

try {
// 拥有自己的constructor属性必然不是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;
}

// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.

var key;
for ( key in obj ) {}

return key === undefined || hasOwn.call( obj, key );
},

测试:
jQuery.isPlainObject({}); // true

你可能感兴趣的:(jquery)