A-A+
jquery的isPlainObject检查是不是对象例子
以前介绍过判断数据的函数,但没讲过检查是不是对象了,如果我们要检查一个对象是否存在要如何做呢?下面一起来看看jquery的isPlainObject用法.
对象一词经常在日常生活中使用,在js中对象{}也是很常用,于是得有个“检查是不是对象”的方法吧,然后jquery提供isPlainObject方法。。。
- //jquery1.11.2
- isPlainObject: function( obj ) {
- var key;
- // Must be an Object.
- // Because of IE, we also have to check the presence of the constructor property.
- // Make sure that DOM nodes and window objects don't pass through, as well
- if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
- return false;
- }
- try {
- // Not own constructor property must be 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;
- }
- // Support: IE<9
- // Handle iteration over inherited properties before own properties.
- if ( support.ownLast ) {
- for ( key in obj ) {
- return hasOwn.call( obj, key );
- }
- }
- // Own properties are enumerated firstly, so to speed up,
- // if last one is own, then all properties are own.
- for ( key in obj ) {}
- return key === undefined || hasOwn.call( obj, key );
- },
测试用例是:
- asyncTest("isPlainObject", function() {
- expect(15);
- var pass, iframe, doc,
- fn = function() {};
- // The use case that we want to match
- ok( jQuery.isPlainObject({}), "{}" );
- // Not objects shouldn't be matched
- ok( !jQuery.isPlainObject(""), "string" );
- ok( !jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number" );
- ok( !jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean" );
- ok( !jQuery.isPlainObject(null), "null" );
- ok( !jQuery.isPlainObject(undefined), "undefined" );
- // Arrays shouldn't be matched
- ok( !jQuery.isPlainObject([]), "array" );
- // Instantiated objects shouldn't be matched
- ok( !jQuery.isPlainObject(new Date()), "new Date" );
- // Functions shouldn't be matched
- ok( !jQuery.isPlainObject(fn), "fn" );
- // Again, instantiated objects shouldn't be matched
- ok( !jQuery.isPlainObject(new fn()), "new fn (no methods)" );
- // Makes the function a little more realistic
- // (and harder to detect, incidentally)
- fn.prototype["someMethod"] = function(){};
- // Again, instantiated objects shouldn't be matched
- ok( !jQuery.isPlainObject(new fn()), "new fn" );
- // DOM Element
- ok( !jQuery.isPlainObject( document.createElement("div") ), "DOM Element" );
- // Window
- ok( !jQuery.isPlainObject( window ), "window" );
- pass = false;
- try {
- jQuery.isPlainObject( window.location );
- pass = true;
- } catch ( e ) {}
- ok( pass, "Does not throw exceptions on host objects" );
- // Objects from other windows should be matched
- Globals.register("iframeDone");
- window.iframeDone = function( otherObject, detail ) {
- window.iframeDone = undefined;
- iframe.parentNode.removeChild( iframe );
- ok( jQuery.isPlainObject(new otherObject()), "new otherObject" + ( detail ? " - " + detail : "" ) );
- start();
- };
- try {
- iframe = jQuery("#qunit-fixture")[0].appendChild( document.createElement("iframe") );
- doc = iframe.contentDocument || iframe.contentWindow.document;
- doc.open();
- doc.write("<body onload='window.parent.iframeDone(Object);'>");
- doc.close();
- } catch(e) {
- window.iframeDone( Object, "iframes not supported" );
- }
- });
以上判断的有dom,window,{},function,null,undefined,new
Object,Number,String,Boolean,Array,Date等,但那么问题来了:
- var obj = {};//这是一个对象吧?
- console.log($.isPlainObject(obj));//true
- var obj2 = {"xuexb": 123};//这也是一个对象吧?
- console.log($.isPlainObject(obj2));//true
- var obj3 = {"nodeType": "啦啦啦"};//这也是一个对象吧?
- console.log($.isPlainObject(obj3));//false
怎么回事呢? 其实原因就在if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {这一判断,jquery认为只要对象的key里有nodeType就是dom,那么俺的对象就不能叫这个了?当然jquery这么做肯定有她的原因,只是俺不知道罢了