Javascript - Template Strings Don't Pretty Print Objects -
can use es6 template strings pretty print javascript objects? react native project, console.log()
outputting chrome debugging tools.
what want
const description = 'app opened'; const properties = { key1: 'val1', blah: 123 }; console.log('description: ', description, '. properties: ', properties);
outputs
template string attempt
// same description , properties const logstring = `description: ${description}. properties: ${properties}`; console.log(logstring);
outputs
question
how first output (with pretty printing) using template strings?
your first example not output string
console
. notice how properties
passed separate parameter argument (as surrounded commas ,
, not string-concatenation operators +
).
when pass object
(or javascript value) console
discrete argument can display how wishes - including interactive formatted display, in first example.
in second example, you're using templated-strings, it's (generally) equivalent this:
logstring = "description: " + description.tostring() + ". properties: " + properties.tostring()";
and object.prototype.tostring()
returns [object object]
default.
in order json (literally javascript object notation) representation of object used in templated string use json.stringify
:
logstring = `description: ${ description }. properties: ${ json.stringify( properties ) }.`
or consider extending tostring
own types:
mypropertiesconstructor.prototype.tostring = function() { return json.stringify( ); };
Comments
Post a Comment