对象自变量:
两种形式:|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;
},