admin管理员组文章数量:1321070
Does this fn:
function isplainobj ( obj ) {
return Object.prototype.toString.call( obj ) === "[object Object]";
}
do the same thing as jQuery's: $.isPlainObject() ?
Does this fn:
function isplainobj ( obj ) {
return Object.prototype.toString.call( obj ) === "[object Object]";
}
do the same thing as jQuery's: $.isPlainObject() ?
Share Improve this question asked Aug 30, 2013 at 11:18 public overridepublic override 9828 silver badges17 bronze badges 2- This should work under certain circumstances. Please show me your snippet where you are using this function. – BVdjV Commented Aug 30, 2013 at 11:35
- im just using it in a general javascript utitlity plugin im working on... – public override Commented Aug 30, 2013 at 11:58
5 Answers
Reset to default 2No, it doesn't.
Here is it's implementation:
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 &&
!core_hasOwn.call(obj, "constructor") &&
!core_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 ( jQuery.support.ownLast ) {
for ( key in obj ) {
return core_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 || core_hasOwn.call( obj, key );
},
It checks if it's type is object, if it has or not a constructor and if its properties are own properties.
For example with your function an instance of a class will return true but in this case, because it has a constructor it will return false.
According to jQuery source code here http://james.padolsey./jquery/#v=1.10.2&fn=jQuery.isPlainObject, I have gone through all possibilities handled in jQuery's plain Object. The function you have mentioned is passing all the tests. And both performing in same way. Please check the below testcases
isplainobj({});
trueisplainobj(function(){});
falseisplainobj(document);
falseisplainobj(window);
falseisplainobj($);
falseisplainobj({});
trueisplainobj(isplainobj);
falseisplainobj({});
true
As per my understanding the object which has all own properties is a plain Object. Please correct me if I am wrong.
Edit:
According to below lines in jQuery
// Not own constructor property must be Object
if (obj.constructor && !core_hasOwn.call(obj, "constructor") && !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
return false;
}
Object should not have own property constructor
and obj.constructor.prototype
should not have isPrototypeOf
to ensure it is not in prototype chain. Please have a look at https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf for more info.
This simple test should do it
obj!=null && typeof(obj)=="object" && Object.getPrototypeOf(obj)==Object.prototype
//to ensure Object.getPrototypeOf
Object.getPrototypeOf||(Object.getPrototypeOf=function(obj){
return obj.__proto__ || obj.prototype || (obj.constructor&&obj.constructor.prototype) || Object.prototype
});
From the jQuery source:
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 &&
!core_hasOwn.call(obj, "constructor") &&
!core_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 ( jQuery.support.ownLast ) {
for ( key in obj ) {
return core_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 || core_hasOwn.call( obj, key );
},
const isPlainObject = obj => (obj?.constructor === Object || Object.getPrototypeOf(obj ?? 0) === null);
console.log(isPlainObject({})); //true
console.log(isPlainObject(Object.create(null))); //true
console.log(isPlainObject({...document.body})); //true
console.log(isPlainObject(window)); //false
console.log(isPlainObject(window._UNDEFINED)); //false
console.log(isPlainObject([1,2,3])); //false
console.log(isPlainObject(new Date)); //false
本文标签: javascriptIsPlainObjectthingStack Overflow
版权声明:本文标题:javascript - IsPlainObject, thing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742091003a2420267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论