jQuery工具方法中的判断方法

来源:互联网 发布:linux时钟中断 编辑:程序博客网 时间:2024/06/06 02:44

对象自变量:

两种形式:|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// - windowif ( 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=814622try {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 Objectreturn true;},
var arr = [],    obj1 = {},    obj2 = new Object();console.log($.isPlainObject(arr));//falseconsole.log($.isPlainObject(obj1));//trueconsole.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;},




原创粉丝点击