Skip to content Skip to sidebar Skip to footer

Get Console.log() To Display Custom Object Description

I have a custom JS object that I've made to represent a grid. Stripped down for this example it looks like this: function Grid(c, r) { var layout = []; var contentPointe

Solution 1:

I have seen that I can add a toString() method to my Grid object in JS but the console does not seem to use it unless I specifically call console.log(aGrid.toString());. While this is fine, I just wondered if there is a way round this

No, there is no way around this. The console alone does decide how to display the passed arguments - and Chrome console lets you dynamically inspect objects instead of trying to serialize them somehow. If you want a custom output, you will need to pass it a string.

Solution 2:

console.log accepts Object as argument(s), and if you put it there, you will be able to inspect it in Chrome console.

So remove .toString() and it will do the trick

console.log("aGird", aGrid );

Solution 3:

You can use console.table() for this. console.table allows you to specify what properties would you like to view. For instance

console.table(aGrid);                            // will show all propertiesconsole.table(aGrid, 'firstName');               // will show only firstName propertyconsole.table(aGrid, ['firstName', 'lastName']); // will show firstName and lastName properties 

Post a Comment for "Get Console.log() To Display Custom Object Description"