Isplainobject Method
Test whether the object is a purely object (created with "{}" or "New Object")
Example:
// test whether the object is purely // true// false
SOURCE Analysis:
function (obj) { // must be-an Object. // Because of IE, we also has to check the presence of the constructor property. // Make sure the DOM Nodes and window objects don ' t pass through, as well if (!obj | | jquery.type (OBJ)!== "Object" | | obj.nodetype | | Jquery.iswindow (obj)) { returnfalse; }
Accept an object to be detected, first listing a few unsatisfied conditions
1.obj can be converted to false
2. Not object type
3. is a DOM object
4. is the Window object
If any one of the above conditions is set to return False
try { // if (Obj.constructor &&!hasown.call (obj, "constructor") &&!hasown.call (obj.constructo R.prototype, "isprototypeof" return false } catch (E) { // ie8,9 would throw exceptions on certain host objects #9897 return false ;}
Because IE will be 8,9 in processing a particular host object will be error, so the use of a try statement, if you go here to explain that the parameter obj must be an object type, but at this time the object type is likely to be created by custom constructors, it is necessary to filter, Represented as a custom constructor if the following conditions are met
1. constructor attribute so far I have not figured out what that means, no matter how you create the object in any way, there is a constructor attribute if you see it here, I want a reasonable explanation.
2. Properties of a parameter constrctor a non-inherited property normally, this property is inherited in the constructor's prototype object, if it is not a description that is manually specified in the constructor
3. If the constructor for the parameter does not have a non-inherited property isprototypeof The description is created by a custom constructor, for a better understanding of the condition below do some code testing:
function Person () {}; Person.prototype={ var obj=New person (); Alert (!! obj.constructor); Alert (!obj.hasownproperty (' constructor ')); Alert (!obj.constructor.prototype.hasownproperty (' isprototypeof '));
Since the custom constructor is filtered, use a custom test to perform the results
true true true
Sure enough, 3 are satisfied, and this will return to false, using the new object method
var obj=New Object ();
The operation results are as follows
true true false
Because the 3rd result is false so it does not seem to return false, the object is created as a literal method
var obj={};
The result of the operation is the same as the new keyword creation. See the results I am very confused, the first condition and the second condition is the use of it? Are returning ture only the third condition is working, isn't it? The first judgment in the "insider" book is interpreted as: "If the object obj does not have a property constructor, then the object is created by the object literal {}, and the object literal creates an object with no constructor property?" There must be some ah, say, even if you can judge but no meaning Ah, the literal object is not in the filter range Ah, I am very confused hope that you give comments to be grateful.
// Own Properties is enumerated firstly // if last one was own, then all properties are own. var key; for inch obj) {} return key = = = Undefined | | Hasown.call (obj, key);
For...in the non-inherited property that is first looped in the loop, and then the inherited property, the propertyisenumerable of the not-inherited attribute must be true. Use this principle if the last property that is looped is an inherited property then return False, If the last is a non-inherited property, then it's definitely a non-inherited property. Returns True
Finally attach the full source:
Isplainobject:function(obj) {//must is an Object. //Because of IE, we also has to check the presence of the constructor property. //Make sure the 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 is Object if(Obj.constructor &&!hasown.call (obj, "constructor") &&!hasown.call (obj.co Nstructor.prototype, "isprototypeof") ) { return false; } } Catch(e) {//ie8,9 'll throw exceptions on certain host objects #9897 return false; } //OWN Properties is enumerated firstly //if last one was own, then all properties are own. varkey; for(Keyinchobj) {} returnKey = = = Undefined | |hasown.call (obj, key); },
Isemptyobject Method
Tests whether the object is an empty object (does not contain any properties).
Example:
// test whether an empty object // true// false
SOURCE Analysis:
function (obj) { forvar in obj) { returnfalse ; } return true ; },
As long as the For loop executes, stating that obj is not null otherwise returns true, that the judgment is straightforward to loop without judging whether the parameter is an object, and you can even use it to determine if it is an empty string
var str= ' str '; var empty= '; alert ($.isemptyobject (str)); // falsealert ($.isemptyobject (empty)); // true
The use and source analysis of jquery static method Isplainobject,isemptyobject method