これは次のように機能しますか:
function isplainobj ( obj ) {
return Object.prototype.toString.call( obj ) === "[object Object]";
}
jQuery と同じことを行います: $.isPlainObject() ?
これは次のように機能しますか:
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 を返します。
ここのjQueryソースコードによるとhttp://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.isPlainObject、jQueryのプレーンオブジェクトで処理されるすべての可能性を調べました。あなたが言及した機能は、すべてのテストに合格しています。そして、どちらも同じように実行します。以下のテストケースを確認してください
isplainobj({});
真実isplainobj(関数(){});
間違いisplainobj(ドキュメント);
間違いisplainobj(ウィンドウ);
間違いisplainobj($);
間違いisplainobj({});
真実isplainobj(isplainobj);
間違いisplainobj({});
真実
私の理解によると、すべての独自のプロパティを持つオブジェクトはプレーンなオブジェクトです。間違っている場合は修正してください。
編集:
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をご覧ください。
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 );
},