简体   繁体   English

我如何将console.log功能与其所有属性一起使用?

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

I'm using the Google Chrome console. 我正在使用谷歌Chrome控制台。 Frustratingly, the following code 令人沮丧的是,以下代码

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

will only log 只会记录

function () {} function(){}

Why does it not print the properties of f , such as fa and f.prototype ? 为什么不打印f的属性,例如faf.prototype How can I print them? 我该如何打印?

试试console.dir

console.dir(f);

console.dir lists all the defined properties of an object. console.dir列出了对象的所有已定义属性。 I think this is probably what you're looking for. 我想这可能就是你要找的东西。

How this appears in FF (Firebug) 如何在FF(Firebug)中出现

Firebug中的console.dir

How this appears in Chromium's console 它是如何出现在Chromium的控制台中的

Chromium中的console.dir

I'm not sure if there's any Chrome documentation on this functionality, but there is Firebug documentation on the console object. 我不确定是否有关于此功能的Chrome文档,但console对象上有Firebug文档

Because a function is not an object. 因为函数不是对象。

If you do: 如果你这样做:

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

You should get what you expect. 你应该得到你期望的。

A function can be a class, but never an object. 函数可以是类,但绝不是对象。 Use new . 使用new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM