0

これは次のように機能しますか:

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

jQuery と同じことを行います: $.isPlainObject() ?

4

4 に答える 4

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 を返します。

于 2013-08-30T11:35:50.030 に答える
2

ここの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をご覧ください。

于 2013-08-30T11:39:23.843 に答える
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 );
},
于 2013-08-30T11:35:13.967 に答える