这是fn:
function isplainobj ( obj ) {
return Object.prototype.toString.call( obj ) === "[object Object]";
}
做与jQuery相同的事情:$ .isPlainObject()?
答案 0 :(得分:2)
不,它没有。
这是它的实施:
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。
答案 1 :(得分:2)
根据jQuery源代码http://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.isPlainObject,我已经完成了在jQuery的普通对象中处理的所有可能性。您提到的功能是通过所有测试。并且两者都以相同的方式表现。请检查以下测试用例
isplainobj({});
真的isplainobj(函数(){});
假的isplainobj(文件);
假的isplainobj(窗口);
假isplainobj($);
假的isplainobj({});
真的isplainobj(isplainobj);
假的isplainobj({});
真的
根据我的理解,具有所有属性的对象是一个普通的Object。如果我错了,请纠正我。
编辑:
根据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;
}
对象不应具有自己的属性constructor
,而obj.constructor.prototype
不应具有isPrototypeOf
以确保它不在原型链中。有关详细信息,请查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf。
答案 2 :(得分:2)
这个简单的测试应该这样做
obj!=null && typeof(obj)=="object" && Object.getPrototypeOf(obj)==Object.prototype
//确保Object.getPrototypeOf
Object.getPrototypeOf||(Object.getPrototypeOf=function(obj){
return obj.__proto__ || obj.prototype || (obj.constructor&&obj.constructor.prototype) || Object.prototype
});
答案 3 :(得分:0)
来自jQuery源代码:
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 );
},