Check whether jquery isPlainObject is an object example

Source: Internet
Author: User

The word "object" is often used in daily life. In js, the object {} is also very common. Therefore, you have to have a method to "check whether it is an object, then jquery provides the isPlainObject method...

// 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 );
},

The test case is:

AsyncTest ("isPlainObject", function (){
CT (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 shocould 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.content?#doc ument;
Doc. open ();
Doc. write ("<body onload = 'window. parent. iframeDone (Object); '> ");
Doc. close ();
} Catch (e ){
Window. iframeDone (Object, "iframes not supported ");
    }
});

The preceding statements include dom, window, {}, function, null, undefined, and new.
Object, Number, String, Boolean, Array, Date, etc., but the problem arises:

Var obj = {}; // Is this an object?
Console. log ($. isPlainObject (obj); // true
Var obj2 = {"xuexb": 123}; // Is this an object?
Console. log ($. isPlainObject (obj2); // true
Var obj3 = {"nodeType": ""}; // Is this an object?
Console. log ($. isPlainObject (obj3); // false


What's going on? The reason is that if (! Obj | jQuery. type (obj )! = "Object" | obj. nodeType | jQuery. isWindow (obj) {this judgment, jquery thinks that as long as the nodeType in the object's key is dom, then the object cannot be called this? Of course, there must be some reason for jquery to do this, but I don't know.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.