A-A+

jquery的isPlainObject检查是不是对象例子

2016年01月09日 前端设计 暂无评论 阅读 6 views 次

以前介绍过判断数据的函数,但没讲过检查是不是对象了,如果我们要检查一个对象是否存在要如何做呢?下面一起来看看jquery的isPlainObject用法.

对象一词经常在日常生活中使用,在js中对象{}也是很常用,于是得有个“检查是不是对象”的方法吧,然后jquery提供isPlainObject方法。。。

  1. //jquery1.11.2  
  2. isPlainObject: function( obj ) {  
  3.     var key;  
  4.     // Must be an Object.  
  5.     // Because of IE, we also have to check the presence of the constructor property.  
  6.     // Make sure that DOM nodes and window objects don't pass through, as well  
  7.     if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {  
  8.         return false;  
  9.     }  
  10.     try {  
  11.         // Not own constructor property must be Object  
  12.         if ( obj.constructor &&  
  13.             !hasOwn.call(obj, "constructor") &&  
  14.             !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {  
  15.             return false;  
  16.         }  
  17.     } catch ( e ) {  
  18.         // IE8,9 Will throw exceptions on certain host objects #9897  
  19.         return false;  
  20.     }  
  21.     // Support: IE<9  
  22.     // Handle iteration over inherited properties before own properties.  
  23.     if ( support.ownLast ) {  
  24.         for ( key in obj ) {  
  25.             return hasOwn.call( obj, key );  
  26.         }  
  27.     }  
  28.     // Own properties are enumerated firstly, so to speed up,  
  29.     // if last one is own, then all properties are own.  
  30.     for ( key in obj ) {}  
  31.     return key === undefined || hasOwn.call( obj, key );  
  32. },  

测试用例是:

  1. asyncTest("isPlainObject"function() {  
  2.     expect(15);  
  3.     var pass, iframe, doc,  
  4.         fn = function() {};  
  5.     // The use case that we want to match  
  6.     ok( jQuery.isPlainObject({}), "{}" );  
  7.     // Not objects shouldn't be matched  
  8.     ok( !jQuery.isPlainObject(""), "string" );  
  9.     ok( !jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number" );  
  10.     ok( !jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean" );  
  11.     ok( !jQuery.isPlainObject(null), "null" );  
  12.     ok( !jQuery.isPlainObject(undefined), "undefined" );  
  13.     // Arrays shouldn't be matched  
  14.     ok( !jQuery.isPlainObject([]), "array" );  
  15.     // Instantiated objects shouldn't be matched  
  16.     ok( !jQuery.isPlainObject(new Date()), "new Date" );  
  17.     // Functions shouldn't be matched  
  18.     ok( !jQuery.isPlainObject(fn), "fn" );  
  19.     // Again, instantiated objects shouldn't be matched  
  20.     ok( !jQuery.isPlainObject(new fn()), "new fn (no methods)" );  
  21.     // Makes the function a little more realistic  
  22.     // (and harder to detect, incidentally)  
  23.     fn.prototype["someMethod"] = function(){};  
  24.     // Again, instantiated objects shouldn't be matched  
  25.     ok( !jQuery.isPlainObject(new fn()), "new fn" );  
  26.     // DOM Element  
  27.     ok( !jQuery.isPlainObject( document.createElement("div") ), "DOM Element" );  
  28.     // Window  
  29.     ok( !jQuery.isPlainObject( window ), "window" );  
  30.     pass = false;  
  31.     try {  
  32.         jQuery.isPlainObject( window.location );  
  33.         pass = true;  
  34.     } catch ( e ) {}  
  35.     ok( pass, "Does not throw exceptions on host objects" );  
  36.     // Objects from other windows should be matched  
  37.     Globals.register("iframeDone");  
  38.     window.iframeDone = function( otherObject, detail ) {  
  39.         window.iframeDone = undefined;  
  40.         iframe.parentNode.removeChild( iframe );  
  41.         ok( jQuery.isPlainObject(new otherObject()), "new otherObject" + ( detail ? " - " + detail : "" ) );  
  42.         start();  
  43.     };  
  44.     try {  
  45.         iframe = jQuery("#qunit-fixture")[0].appendChild( document.createElement("iframe") );  
  46.         doc = iframe.contentDocument || iframe.contentWindow.document;  
  47.         doc.open();  
  48.         doc.write("<body onload='window.parent.iframeDone(Object);'>");  
  49.         doc.close();  
  50.     } catch(e) {  
  51.         window.iframeDone( Object, "iframes not supported" );  
  52.     }  
  53. });  

以上判断的有dom,window,{},function,null,undefined,new

Object,Number,String,Boolean,Array,Date等,但那么问题来了:

  1. var obj = {};//这是一个对象吧?  
  2. console.log($.isPlainObject(obj));//true  
  3. var obj2 = {"xuexb": 123};//这也是一个对象吧?  
  4. console.log($.isPlainObject(obj2));//true  
  5. var obj3 = {"nodeType""啦啦啦"};//这也是一个对象吧?  
  6. console.log($.isPlainObject(obj3));//false  

怎么回事呢? 其实原因就在if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {这一判断,jquery认为只要对象的key里有nodeType就是dom,那么俺的对象就不能叫这个了?当然jquery这么做肯定有她的原因,只是俺不知道罢了

标签:

给我留言