node.js控制台_如何在node.js中使⽤控制台
node.js 控制台
( )
In this article, we'll learn how to use most methods available in the nodeJS console class more effectively.
在本⽂中,我们将学习如何更有效地使⽤nodeJS控制台类中可⽤的⼤多数⽅法。
To demonstrate, I'll use Chrome browser version 70.0.3538.77 and nodeJS version 8.11.** 3**.
为了演⽰,我将使⽤Chrome浏览器70版。 0 。 3538 。 77和nodeJS版本8 。 11 。** 3 **。
Okay then. Let's begin.
好吧。 让我们开始。
( )
console.log(string, substitution)
While the famed console.log method really needs no introduction, you'll be glad to know that the console.info and
console.debug methods are identical to console.log in their operation.
虽然确实不需要介绍著名的console.log⽅法,但是您会很⾼兴知道console 。 信息和控制台 。 调试⽅法与console相同。 登录他们的操作。
You can use console.debug in the browser console by default but to use it in , you'll have to set the log level to verbose like this.
您可以使⽤console 。 默认情况下,在浏览器控制台中进⾏调试 ,但要在使⽤它,则必须将⽇志级别设置为冗长,如下所⽰。
The console.log method prints to standard out, whether this be the terminal or browser console. It outputs strings by default but can be used in conjuction with template strings to modify what it returns.
控制台 。 log⽅法输出到标准输出,⽆论是终端还是浏览器控制台。 默认情况下,它输出字符串,但可以与模板字符串结合使⽤以修改其返回的内容。
Here's how it works:
运作⽅式如下:
The arguments in the template string are passed to which then processes the arguments by replacing each substitution token with the respective converted value. The supported substitution tokens are:
模板字符串中的参数传递给 ,然后通过将每个替换标记替换为各⾃的转换值来处理参数。 ⽀持的替换令牌是:%s (%s)
const msg = `Using the console class`;
console.log('%s', msg); // Using the console class
console.log(msg); // Using the console class
%s is the default substitution pattern.
%s是默认的替换模式。
%d,%f,%i,%o (%d, %f, %i, %o)
const circle = (radius = 1) => {
const profile = {};
const pi = 22/7;
profile.diameter = 2_pi_radius;
profile.circumference = pi_radius_2;
profile.area = pi_radius^2;
profile.volume = 4/3_pi_radius^3;
console.log('This circle has a radius of: %d cm', radius);
console.log('This circle has a circumference of: %f cm', profile.diameter);
console.log('This circle has an area of: %i cm^2', profile.area);
console.log('The profile of this cirlce is: %o', profile);
console.log('Diameter %d, Area: %f, Circumference %i', profile.diameter, profile.area, profile.circumference)
}
circle();
This is what happens:
这是发⽣了什么:
%d will be substituted by a digit (integer or float). %f will be replaced by a float value. %i will be replaced by an integer. %o will be replaced by an Object.
%d将替换为数字(整数或浮点数)。 %f将被浮点值替换。 %i将被替换为整数。 %o将被Object取代。
%o is especially handy because we don't have to use to expand our object because it shows all the object's properties by default.
%o特别⽅便,因为我们不必使⽤来扩展对象,因为它默认情况下会显⽰所有对象的属性。
Note that you can use as many token substitutions as you like. They'll just be replaced the same order as the arguments you pass.
请注意,您可以根据需要使⽤任意数量的令牌替换。 它们将被替换为与您传递的参数相同的顺序。
%C (%c)
This substitution token applies css styles to the subsituted text.
此替换标记将CSS样式应⽤于替换的⽂本。
console.log('LOG LEVEL: %c OK', 'color: green; font-weight: normal');
console.log('LOG LEVEL: %c PRIORITY', 'color: blue; font-weight: medium');
console.log('LOG LEVEL: %c WARN', 'color: red; font-weight: bold');
console.log('ERROR HERE');
Here it is in action.
它在起作⽤。
js arguments
Above, we note that the text we pass to console.log after the %c substitution token is affected by the styles, but the text before is left as is without styling.
上⾯,我们注意到%c替换标记之后传递给console.log的⽂本受样式影响,但之前的⽂本保留不样式的样式。
( )
The first argument passed to it is the data to be returned in the form of a table. The second is an array of selected columns to be displayed.
传递给它的第⼀个参数是要以表形式返回的数据。 第⼆个是要显⽰的选定列的数组。
console.table(tabularData, [properties])
This method will print the input passed to it formatted as a table then log the input object after the table representation.
此⽅法将打印传递给它的输⼊格式为表,然后在表表⽰形式之后记录输⼊对象。
数组 (Arrays)
If an array is passed to it as data, each element in the array will be a row in the table.
如果将数组作为数据传递给它,则数组中的每个元素将是表中的⼀⾏。
const books = ['The Silmarillion', 'The Hobbit', 'Unfinished Tales'];
console.table(books);
With a simple array with a depth of 1, the first column in the table has the heading index. Under the index header in the first column are the array indexes and the items in the array are listed in the second column under the value **header.**
对于深度为1的简单数组,表中的第⼀列的标题为 。 第⼀列的索引标头下⽅是数组索引,第⼆列中的数组值列在** header 。**下。
This is what happens for a nested array.
这就是嵌套数组发⽣的情况。
const authorsAndBooks = [['Tolkien', 'Lord of The Rings'],['Rutger', 'Utopia For Realists'], ['Sinek', 'Leaders Eat Last'], ['Eyal', 'Habit']];
console.table(authorsAndBooks);
对象 (Objects)
For objects with a depth of 1, the object keys will be listed under the index header and the values in the object under the second column header.
对于深度为1的对象,对象键将在索引标题下列出,⽽对象中的值将在第⼆列标题下列出。
const inventory = { apples: 200, mangoes: 50, avocados: 300, kiwis: 50 };
console.table(inventory);
For nested objects,
对于嵌套对象,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。