cuốn sách gpt4 ai đã làm

javascript - IsPlainObject,东西?

In lại Tác giả: Walker 123 更新时间:2023-11-28 12:34:32 26 4
mua khóa gpt4 Nike

这个fn是吗:

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

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

câu trả lời hay nhất

不,事实并非如此。

这是它的实现:

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 ) ) {
trả về sai;
}

thử {
// Not own constructor property must be Object
if ( obj.constructor &&
!core_hasOwn.call(obj, "constructor") &&
!core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
trả về sai;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
trả về sai;
}

// 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
Đề xuất bài viết: css - 让两个
重叠?
Đề xuất bài viết: css - 带侧页眉的粘性页脚
Đề xuất bài viết: javascript - 灯箱将图像移动到右下角
Đề xuất bài viết: PHP 智能 || (OR) 运算符就像 JavaScript 中一样
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress