Randomblue
Randomblue

Reputation: 116353

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

I'm using the Google Chrome console. Frustratingly, the following code

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

will only log

function () {}

Why does it not print the properties of f, such as f.a and f.prototype? How can I print them?

Upvotes: 9

Views: 2616

Answers (3)

Halcyon
Halcyon

Reputation: 57703

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.

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237975

console.dir lists all the defined properties of an object. I think this is probably what you're looking for.

How this appears in FF (Firebug)

console.dir in Firebug

How this appears in Chromium's console

console.dir in Chromium

I'm not sure if there's any Chrome documentation on this functionality, but there is Firebug documentation on the console object.

Upvotes: 3

Anurag
Anurag

Reputation: 141899

Try console.dir.

console.dir(f);

Upvotes: 16

Related Questions