lovespring
lovespring

Reputation: 19559

How to show full object in Chrome console

var functor=function(){
    //test
}

functor.prop=1;

console.log(functor);

this only show the function part of the functor, cannot show the properties of the functor in console.

Upvotes: 170

Views: 197830

Answers (9)

vanowm
vanowm

Reputation: 10201

Another method is to wrap function into an array:

console.log( [functor] );

Upvotes: 0

Jens Törnell
Jens Törnell

Reputation: 24748

I made a function of the Trident D'Gao answer.

function print(obj) {
  console.log(JSON.stringify(obj, null, 4));
}

How to use it

print(obj);

Upvotes: 5

Trident D'Gao
Trident D'Gao

Reputation: 19690

You might get even better results if you try:

console.log(JSON.stringify(obj, null, 4));

Upvotes: 38

akim
akim

Reputation: 8759

With modern browsers, console.log(functor) works perfectly (behaves the same was a console.dir).

Upvotes: 0

Kean Amaral
Kean Amaral

Reputation: 5957

var gandalf = {
  "real name": "Gandalf",
  "age (est)": 11000,
  "race": "Maia",
  "haveRetirementPlan": true,
  "aliases": [
    "Greyhame",
    "Stormcrow",
    "Mithrandir",
    "Gandalf the Grey",
    "Gandalf the White"
  ]
};
//to console log object, we cannot use console.log("Object gandalf: " + gandalf);
console.log("Object gandalf: ");
//this will show object gandalf ONLY in Google Chrome NOT in IE
console.log(gandalf);
//this will show object gandalf IN ALL BROWSERS!
console.log(JSON.stringify(gandalf));
//this will show object gandalf IN ALL BROWSERS! with beautiful indent
console.log(JSON.stringify(gandalf, null, 4));

Upvotes: 15

John Henckel
John Henckel

Reputation: 11347

I wrote a function to conveniently print things to the console.

// function for debugging stuff
function print(...x) {
    console.log(JSON.stringify(x,null,4));
}

// how to call it
let obj = { a: 1, b: [2,3] };
print('hello',123,obj);

will output in console:

[
    "hello",
    123,
    {
        "a": 1,
        "b": [
            2,
            3
        ]
    }
]

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

Use console.dir() to output a browse-able object you can click through instead of the .toString() version, like this:

console.dir(functor);

Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed [1]


[1] https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference#dir

Upvotes: 273

domSurgeon
domSurgeon

Reputation: 91

this worked perfectly for me:

for(a in array)console.log(array[a])

you can extract any array created in console for find/replace cleanup and posterior usage of this data extracted

Upvotes: 9

BastiBen
BastiBen

Reputation: 19860

You might get better results if you try:

console.log(JSON.stringify(functor));

Upvotes: 126

Related Questions