这个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 ) ) {
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。
Tôi là một lập trình viên xuất sắc, rất giỏi!