javascript - How to check if an argument is an object? -
i know considered object in javascript. making function requires argument accepts in format:
{ a: 'b', c: 'd' } so type of key value pair inside curly braces sort of object not other type of objects. how validate specifically?
update: after writing of below , later giving further thought, i'd suggest take @ trevor's answer. think more deserving of beautiful green checkmark answer, , here's why.
i took question @ face value , ran it: if want literally said, function $.isplainobject() way go. really need do? keep in mind $.isplainobject() not efficient function; enumerates through object's properties. , noted below, can't distinguish between {} , new object().
instead of that, trevor suggests checking properties need, , cases, of time, think he's right. it's closer approach take in own code: validate need, ignore rest.
so take @ answer , consider it. (and won't offended if move checkmark!)
now original answer:
there's $.isplainobject() function in jquery. if you're not using jquery, can copy source code function app. open jquery source , search isplainobject:.
it's worth reading source function in case, see isn't simple typeof or instanceof check:
isplainobject: function( obj ) { // must object. // because of ie, have check presence of constructor property. // make sure dom nodes , window objects don't pass through, if ( !obj || jquery.type(obj) !== "object" || obj.nodetype || jquery.iswindow( obj ) ) { return false; } try { // not own constructor property must object if ( obj.constructor && !core_hasown.call(obj, "constructor") && !core_hasown.call(obj.constructor.prototype, "isprototypeof") ) { return false; } } catch ( e ) { // ie8,9 throw exceptions on host objects #9897 return false; } // own properties enumerated firstly, speed up, // if last 1 own, properties own. var key; ( key in obj ) {} return key === undefined || core_hasown.call( obj, key ); }, type: function( obj ) { if ( obj == null ) { return string( obj ); } return typeof obj === "object" || typeof obj === "function" ? class2type[ core_tostring.call(obj) ] || "object" : typeof obj; }, iswindow: function( obj ) { return obj != null && obj == obj.window; }, and there class2type object code uses:
// [[class]] -> type pairs class2type = {}, // populate class2type map jquery.each("boolean number string function array date regexp object error".split(" "), function(i, name) { class2type[ "[object " + name + "]" ] = name.tolowercase(); }); (these code snippets out of context; you'd need tweak syntax if using them standalone.)
of course depending on circumstances, may not need all of these checks. , if you're using jquery already, can call $.isplainobject() , not sweat details.
also note not possible distinguish object literal object created new object:
var obj1 = { a:'b', c:'d' }; var obj2 = new object(); obj2.a = 'b'; obj2.c = 'd'; console.log( $.isplainobject(obj1) ); // prints true console.log( $.isplainobject(obj2) ); // prints true! they both return true $.isplainobject(), , i'm pretty sure other test devise unable tell which either.
a funny historical note: when john resig added function jquery in 2009, going call $.isobjectliteral(). @ urging changed name $.isplainobject because of ambiguity.
Comments
Post a Comment