JavaScript Console对象调试程序

Console对象被用于JavaScript调试;

JavaScript 中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象;

1. 输出信息

console.log('普通信息');      //输出普通信息
console.info('提示信息');     //输出提示信息
console.error('错误信息');    //输出错误信息
console.warn('警告信息');     //输出警告信息

注:输出信息支持printf的占位符格式,支持的占位符有:字符(%s)、整数(%d或%i)、浮点数(%f)和对象(%o);

例如:console.log("%d年%d月%d日",2011,3,26);

2. 分组输出信息

使用方法group()和groupEnd();

console.group('1班');
    console.group('1组');
        console.log('zhu');
        console.log('sheng');
        console.log('tom');
    console.groupEnd();
    console.group('2组');
        console.log('stephen');
        console.log('curry');
        console.groupEnd();
    console.groupEnd()
console.groupEnd()



//输出结果
1班
  1组
    zhu
    sheng
    tom
  2组
    stephen
    curry

3. 输出代码运行的次数

函数原型:

count(label?: string): void;
for(var i=1; i<10; i++)
{
    console.count('运行次数');
}

//输出结果
运行次数: 1
运行次数: 2
运行次数: 3
运行次数: 4
运行次数: 5
运行次数: 6
运行次数: 7
运行次数: 8
运行次数: 9

4. 数据表格化输出

使用table方法;

var list = [
    {name:'stephen curry', old:33, 位置:'PG'},
    {name:'klay thompson', old:29, 位置:'SG'}
];
console.table(list);

//输出结果
┌─────────┬─────────────────┬─────┬──────┐
│ (index) │      name       │ old │ 位置  │
├─────────┼─────────────────┼─────┼──────┤
│    0    │ 'stephen curry' │ 33  │ 'PG' │
│    1    │ 'klay thompson' │ 29  │ 'SG' │
└─────────┴─────────────────┴─────┴──────┘

5. 统计代码执行时间

使用time方法和timeEnd方法;

console.time("运行时间");
var count=0;
for(var i=1; i<100000; i++)
{
    count++;
}
console.timeEnd("运行时间");

//输出结果
运行时间: 3.757ms

6. 清除控制台的所有输出

使用clear()方法;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值