How can I make console.log show the current state of an object? Ask Question

How can I make console.log show the current state of an object? Ask Question

アドオンのない Safari (および実際には他のほとんどのブラウザ) では、呼び出されたconsole.logときの状態ではなく、実行の最後の状態でオブジェクトが表示されます。console.log

console.logその行のオブジェクトの状態を取得するには、オブジェクトを複製して出力する必要があります。

例:

var test = {a: true}
console.log(test); // {a: false}
test.a = false; 
console.log(test); // {a: false}

ベストアンサー1

あなたが探しているのは だと思いますconsole.dir()

console.log()オブジェクトへの参照を出力し、それを開くまでに変更されているため、必要な動作は実行されませんconsole.dir。呼び出した時点でのオブジェクト内のプロパティのディレクトリを出力します。

以下の JSON のアイデアは良いものです。JSON 文字列を解析して、.dir() で得られるような参照可能なオブジェクトを取得することもできます。

console.log(JSON.parse(JSON.stringify(obj)));

おすすめ記事