そこでisPlainObject、JavaScript オブジェクト リテラルをテストするために使用する簡単な方法を用意しました。
var isPlainObject = function (obj) {
return typeof obj === "object" && {}.toString.call(obj) === "[object Object]";
};
今、私はプレーンなオブジェクトを持っています:
var obj = {'one': 1, 'two': 2, 'three': 3};
isPlainObject(obj)関数を実行すると、期待どおりに動作し、 が返されますtrue。私の質問は、オブジェクトのプロトタイプにプロパティを追加することから来ています:
obj.constructor.prototype.four = 4;
実行isPlainObject(obj)するobjと、 が返されますfalse。両方のインスタンスでtypeof obj返されます。プロトタイプにプロパティを追加した後、2 番目のインスタンスで返されobjectますtoString。[object Number]
何が変わったのobjですか?どうしたの?
編集: これは、 QUnit関数呼び出しの範囲内でテストされた場合にのみ発生します。
test("each", function() {
_.each([1, 2, 3], function(i,v) {
equal(v, i + 1, 'each iterator provided index and value from array');
});
var obj = {'one': 1, 'two': 2, 'three': 3};
console.log(_.isPlainObject(obj)); // => true
obj.constructor.prototype.four = 4;
console.log(_.isPlainObject(obj)); // => false
});
arguments編集:これは、配列のようなオブジェクトを 内でログに記録するときに取得する console.log ですisPlainObject。

ログを見ると、配列に 2 つの引数があることがわかります。しかし、長さはまだ1.