zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Javascript] Format console.log with CSS and String Template Tags

JavaScriptlogCSS string and with format Template
2023-09-14 08:59:18 时间

The Chrome console allows you to format messages using CSS properties. This lesson walks you through the syntax of formatting your logs with css then refactoring into a template tag function to make formatting more reusable.

 

const debug = (label, style, obj) => {
    console.log(`%c${label}`, `${style}`, `${obj}`);
};

debug(
    `debug: `,
    `color: white; background-color: grey; padding: 0 0.5em;`,
    JSON.stringify({name: 'Zhentian'}));