簡體   English   中英

我如何將console.log功能與其所有屬性一起使用?

[英]How can I console.log functions alongside all their properties?

我正在使用谷歌Chrome控制台。 令人沮喪的是,以下代碼

var f = function () {};
f.a = 1;
console.log(f);

只會記錄

function(){}

為什么不打印f的屬性,例如faf.prototype 我該如何打印?

試試console.dir

console.dir(f);

console.dir列出了對象的所有已定義屬性。 我想這可能就是你要找的東西。

如何在FF(Firebug)中出現

Firebug中的console.dir

它是如何出現在Chromium的控制台中的

Chromium中的console.dir

我不確定是否有關於此功能的Chrome文檔,但console對象上有Firebug文檔

因為函數不是對象。

如果你這樣做:

var f = function () {};
var my_instance = new f();  // aha!
my_instance.a = 1;
console.log(my_instance);

你應該得到你期望的。

函數可以是類,但絕不是對象。 使用new

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM