gpt4 book ai didi

javascript - IsPlainObject,东西?

转载 作者:行者123 更新时间:2023-11-28 12:34:32 26 4
gpt4 key购买 nike

这个fn是吗:

function isplainobj ( obj ) {
return Object.prototype.toString.call( obj ) === "[object Object]";
}

做与 jQuery 相同的事情:$.isPlainObject() ?

最佳答案

不,事实并非如此。

这是它的实现:

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

它检查它的类型是否是对象,它是否有构造函数以及它的属性是否是自己的属性。

例如,对于您的函数,类的实例将返回 true,但在这种情况下,因为它有一个构造函数,它将返回 false。

关于javascript - IsPlainObject,东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531624/

26 4 0
文章推荐: css - 让两个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com