美文网首页JS
console简单整理

console简单整理

作者: 大盗老袁 | 来源:发表于2017-06-26 09:58 被阅读6次

    向控制台输出信息四种不同方式:

    • log
    • info
    • warn
    • error

    替换

    格式化说明符:

    • %s | 字符串替换元素
    • %(d|i) | 整数代替元素
    • %f | 浮点数代替元素
    • %(o|O) 元素显示为一个对象
    • %c 应用提供的css

    例如:console.log('int: %d, floating-point: %.1f', 1, 1.5)

    console.log('this is an object %o', { obj: { obj2: 'hello' }})

    模板文字:

    const a ='substitution';
    console.log(`bear:$ {a}`);
    // bear:substitution
    

    特殊的方法

    Dir():

    console.dir(document.body);

    显示对象所有的属性和方法

    Table():

    以表格形式显示数组或者对象

    console.table(['Javascript', 'PHP', 'Perl', 'C++']);

    time():

    console.time('test1');
    
    for(var i=0;i<20;i++){
      if(i%3==2)
       console.log(i);
    }
    
    console.timeEnd('test1');
    
    /*test1: timer startedScratchpad/1:10
    2  Scratchpad/1:14:4
    5  Scratchpad/1:14:4
    8  Scratchpad/1:14:4
    11  Scratchpad/1:14:4
    14  Scratchpad/1:14:4
    17  Scratchpad/1:14:4
    test1: 2.39ms*/
    

    摘自:

    how to get the most out of the js console

    相关文章

      网友评论

        本文标题:console简单整理

        本文链接:https://www.haomeiwen.com/subject/dspdcxtx.html