js - console

参考资料

JavaScript Console 对象

Node.js console.debug()用法及代码示例

nodejs.org

console.log输出字体颜色

5分钟教你使用 console.log 输出五彩斑斓的黑

Console 对象方法

assert()

assert方法接受两个参数,第一个参数是表达式,第二个参数是字符串。只有当第一个参数为false,才会输出第二个参数,否则不会有任何结果。

// 实例
console.assert(true === false, "判断条件不成立")
// Assertion failed: 判断条件不成立
console.assert(false, "判断条件不成立")
// Assertion failed: 判断条件不成立
console.assert(true, "判断条件不成立")
//

clear()

清除当前控制台的所有输出,将光标回置到第一行。

console.clear()

count()

用于计数,输出它被调用了多少次。

(function() {
for (var i = 0; i < 5; i++) {
console.count('count');
}
})();
// count: 1
// count: 2
// count: 3
// count: 4
// count: 5

error()

输出信息时,在最前面加一个红色的叉,表示出错,同时会显示错误发生的堆栈。

console.error("Error: %s (%i)", "Server is not responding",500)
// Error: Server is not responding (500)

group() groupCollapsed() groupEnd()

  • group 用于将显示的信息分组,可以把信息进行折叠和展开。
  • groupCollapsed 与console.group方法很类似,唯一的区别是该组的内容,在第一次显示时是收起的(collapsed),而不是展开的。
  • groupEnd 结束内联分组
// group
console.group('第一层');
console.group('第二层'); console.log('error');
console.error('error');
console.warn('error'); console.groupEnd();
console.groupEnd(); // groupCollapsed
console.groupCollapsed('第一层');
console.groupCollapsed('第二层'); console.log('error');
console.error('error');
console.warn('error'); console.groupEnd();
console.groupEnd();

info()

console.log 别名,输出信息

console.info("runoob")

log()

输出信息

console.log("runoob")

table()

将复合类型的数据转为表格显示。


var arr= [
{ num: "1"},
{ num: "2"},
{ num: "3" }
];
console.table(arr); var obj= {
a:{ num: "1"},
b:{ num: "2"},
c:{ num: "3" }
};
console.table(obj); console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
// ┌─────────┬─────┬─────┐
// │ (index) │ a │ b │
// ├─────────┼─────┼─────┤
// │ 0 │ 1 │ 'Y' │
// │ 1 │ 'Z' │ 2 │
// └─────────┴─────┴─────┘ console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);
// ┌─────────┬─────┐
// │ (index) │ a │
// ├─────────┼─────┤
// │ 0 │ 1 │
// │ 1 │ 'Z' │
// └─────────┴─────┘

time() timeEnd()

计时开始 计时结束

console.time('计时器1');
for (var i = 0; i < 100; i++) {
for (var j = 0; j < 100; j++) {}
}
console.timeEnd('计时器1');
console.time('计时器2');
for (var i = 0; i < 1000; i++) {
for (var j = 0; j < 1000; j++) {}
}
console.timeEnd('计时器2');

trace()

追踪函数的调用过程

function d(a) {
console.trace();
return a;
}
function b(a) {
return c(a);
}
function c(a) {
return d(a);
}
var a = b('123');

warn()

输出警告信息

console.warn("警告")

debug()

控制台模块的内置应用程序编程接口,用于将消息打印到换行符中的stdout。与console.log()方法类似。

console.debug(data, args);

参数:此方法具有上述和以下所述的两个参数:

  • data:此参数指定要打印的数据。
  • args:这是可选参数,用于指定要作为替换值在传递给数据的消息中传递的参数。所有传递的参数均发送到util.format()。

返回值:此方法不会返回任何内容,只会在换行符中将格式化后的消息打印到stdout。

以下示例说明了Node.js中console.debug()方法的用法:

// app.js
// Accessing console module
const console = require('console'); // Calling console.debug()
console.debug("This is a sample debug message!");
console.debug("Sample debug message with args:%d", 39); // 使用以下命令运行app.js文件:
node app.js // 输出:
This is a sample debug message!
Sample debug message with args:39

注意:上面的程序将通过使用以下命令进行编译和运行node filename.js命令。

占位符

占位符 作用
%s 字符串
%d or %i 整数
%f 浮点数
%o 可展开的DOM
%O 列出DOM的属性
%c 根据提供的css样式格式化字符串
// %c表示css样式
console.log('%cHello', 'color: #43bb88;font-size: 24px;font-weight: bold;text-decoration: underline;');
// **更灵活的调用 console 方法**
// 通过观察上面的用法,我们发现如果我们需要给同一个字符串添加更多的 CSS 效果,我们需要编辑 log 的第二个参数,而如果需要把两个有颜色的字符串拼接到一起,则需要修改第一参数,并且添加一个第三参数。
console.log(`%c123%c456`,'color: blue;','color: green;')
// %d表示数字
console.log('%d', 123); // %i表示整型数字
console.log('%i', 123); // %o表示DOM元素
console.log('%o', document.body); // %O表示javascript对象
console.log('%O', new Date());

js - console的更多相关文章

  1. js console API All In One

    js console API All In One const log = console.log; for(const key in console) { log(`navigator.${key} ...

  2. js console 性能测试 & don't-use-array-foreach-use-for-instead

    don't-use-array-foreach-use-for-instead slower https://coderwall.com/p/kvzbpa/don-t-use-array-foreac ...

  3. js console.log all in one

    js console.log all in one this & arguments "use strict"; /** * * @author xgqfrms * @li ...

  4. js console.log color all in one

    js console.log color all in one console.log color Chrome console.log 语法 / grammar %c, %s, css style ...

  5. 浏览器js console对象

    js中调用console写日志 console.log("some log"); console.warn("some warning"); console.e ...

  6. js console.log 打印 对像 数组 详解

    console.log是什么东西,其实就是一个打印js数组和对像的函数而已,就像是php的print_r,var_dump.console.log这个函数本身没什么好说的,这篇博客告诉大家怎么去用这个 ...

  7. js console对象

    js调试 根据信息的不同性质,console对象显示信息的方法,分别是一般信息console.log(),console.info().除错信息console.debug().警告提示console. ...

  8. js console一些常用的功能

    前言 很多时候我们在调试的时候经常用console.log,我感觉其实一个就够了,但是有时候你不可能写一步就去调试下,所以呢,经常用几个console.log,有时候挺难找的,后面翻了翻console ...

  9. js console 一些拓展技巧

    console.time 方法 / console.timeEnd() 方法 统计一段代码的执行时间, 形参必须一致 console.time("string"); for(var ...

  10. [JS] console.time() - 计时器构造函数及如何计时

    概述 使用计时器可以对代码运行过程进行测速.你可以给每个计时器取一个名字,每个页面上最多可以运行一万个计时器.当你使用计时器名字调用 console.timeEnd() 函数时,浏览器会返回一个毫秒值 ...

随机推荐

  1. React 使用链表遍历组件树

    React 为在有限的资源情况下,更好地控制UI的更新,提出了时间分片的概念.以达到三个目标: performing non-blocking rendering(无阻塞渲染):applying up ...

  2. 分享至: 日本神話の考古学.PDF

    书本详情 日本神話の考古学 种类:Languages - General & Miscellaneous Languages - Reference年:1993出版社:朝日新聞社语言:japa ...

  3. hdu 1516 String Distance and Transform Process

    Problem DescriptionString Distance is a non-negative integer that measures the distance between two ...

  4. ES实战-trying to create too many buckets

    场景 es查询报错,报错如下: trying to create too many buckets. must be less than or equal to: [10000] but was [1 ...

  5. uniapp打包h5

    1. 找到项目中 manifest.json --- H5 配置---运行时的基础路径, 将路径修改为 相对路径(./ ) 注意: 1.运行的基础路径系统默认打包路径为绝对路径,如不改,打包时找不到对 ...

  6. uni-app 声音/震动提示,播放系统默认消息声音 安卓(Android)测试通过

    可以跟据自己需求使用,有不足的地方希望大家帮忙补充 参数 type Options vibrate:震动 othre:铃声 1 msg_remind(type) 2 { 3 if(type=='vib ...

  7. Linux常用的操作指令01

    关键字: linux 查进程.杀进程.起进程1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.   ...

  8. QE11 / QE51N 界面太小问题

    修复后界面是,修复前常规页签中的数据只能显示4行,需要的note是 2639352 , SNOTE 进行打补丁就好  note是 2639352

  9. CAD动态输入框不见了怎么办?教你三个调出方法,轻松搞定!

    CAD动态输入是除了命令行以外又一种友好的人机交互方式,在CAD设计过程中,启用CAD动态输入功能,可以直接在光标附近显示信息.输入值等.可当CAD动态输入框不见了的时候,该怎么办呢?本文小编以浩辰C ...

  10. linux的打开文件标志O_CLOEXEC

    当没有这个标志,打开文件时,得到的fd, 将会被子进程继承,并且子进程会获得这个fd的读写能力. 往往父进程打开的文件,不希望子进程读写,所以,子进程启动之后,可以手动关闭fd. 但是关闭fd的操作不 ...