一、显示信息的命令
<script type="text/javascript">
console.log('hello');
console.info('信息');
console.error('错误');
console.warn('警告');
</script>

二、占位符
<script type="text/javascript">
console.log("%d年%d月%d日",2017,4,06);
</script>
占位符有:字符(%s)、整数(%d或%i)、浮点数(%f)和对象(%o)

格式如下:

console.log("%c 这是谁的地址? ", "blog.csdn.net/aerchi");

三、信息分组
<script type="text/javascript">
console.group("第一组信息");
console.log("第一组第一条");
console.log("第一组第二条");
console.groupEnd();

console.group("第二组信息");
console.log("第二组第一条");
console.log("第二组第二条");
console.groupEnd();
</script>

四、查看对象的信息
<script type="text/javascript">
var info = {
QQ:27 518 3630,
E-Mail:"aerchi@live.com"
};
console.dir(info);
</script>

五、显示某个节点的内容
<div id="info">
<p>QQ:27 518 3630<p>
<p>Eail:aerchi@live.com</p>
</div>
<script type="text/javascript">
var info = document.getElementById('info');
console.dirxml(info);
</script>

六、判断变量是否是真

请参考: https://developer.mozilla.org/en-US/docs/Web/API/console/assert
<script type="text/javascript">

console.assert(assertion, obj1 [, obj2, ..., objN]);
console.assert(assertion, msg [, subst1, ..., substN]); // c-like message 

</script>

七、追踪函数的调用轨迹
<script type="text/javascript">
   /*函数是如何被调用的,在其中加入console.trace()方法就可以了*/
  function add(a,b){
           console.trace();
    return a+b;
  }
  function one(a,b){return add(a,b);}
  function two(a,b){return add(a,b);}
    function three(a,b){return add(a,b);}
</script>

八、计时功能
<script type="text/javascript">
console.time("计时器开始");
for(var i=0;i<1000;i++){
for(var j=0;j<1000;j++){
……
}
}
console.timeEnd("计时器结束");
</script>

九、console.profile()的性能分析
<script type="text/javascript">
console.profile('性能分析器');
Function();
console.profileEnd();
</script>

十、版权信息

<script type="text/javascript">
console.log("%cPowered by\r\nhttp://blog.csdn.net/aerchi", " text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),/*aerchi*/0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:2em");
</script>

以上输出如下:

Methods

Console.assert()
Log a message and stack trace to console if first argument is false.
Console.clear()
Clear the console.
Console.count()
Log the number of times this line has been called with the given label.
Console.debug()
An alias for log()
Console.dir() 
Displays an interactive listing of the properties of a specified JavaScript object. This listing lets you use disclosure triangles to examine the contents of child objects.
Console.dirxml() 

Displays an XML/HTML Element representation of the specified object if possible or the JavaScript Object view if it is not.

Console.error()
Outputs an error message. You may use string substitution and additional arguments with this method.
Console.exception()  
An alias for error()
Console.group()
Creates a new inline group, indenting all following output by another level. To move back out a level, call groupEnd().
Console.groupCollapsed()
Creates a new inline group, indenting all following output by another level; unlike group(), this starts with the inline group collapsed, requiring the use of a disclosure button to expand it. To move back out a level, call groupEnd().
Console.groupEnd()
Exits the current inline group.
Console.info()
Informative logging information. You may use string substitution and additional arguments with this method.
Console.log()
For general output of logging information. You may use string substitution and additional arguments with this method.
Console.profile() 
Starts the browser's build-in profiler (for example, the Firefox performance tool). You can specify an optional name for the profile.
Console.profileEnd() 
Stops the profiler. You can see the resulting profile in the browser's performance tool (for example, the Firefox performance tool).
Console.table()
Displays tabular data as a table.
Console.time()
Starts a timer with a name specified as an input parameter. Up to 10,000 simultaneous timers can run on a given page.
Console.timeEnd()
Stops the specified timer and logs the elapsed time in seconds since its start.
Console.timeStamp() 
Adds a marker to the browser's Timeline or Waterfall tool.
Console.trace()
Outputs a stack trace.
Console.warn()
Outputs a warning message. You may use string substitution and additional arguments with this method.

Usage

Outputting text to the console

The most frequently-used feature of the console is logging of text and other data. There are four categories of output you can generate, using the console.log()console.info()console.warn(), and console.error() methods. Each of these results in output that's styled differently in the log, and you can use the filtering controls provided by your browser to only view the kinds of output that interest you.

There are two ways to use each of the output methods; you can simply pass in a list of objects whose string representations get concatenated into one string, then output to the console, or you can pass in a string containing zero or more substitution strings followed by a list of the objects with which to replace them.

Outputting a single object

The simplest way to use the logging methods is to output a single object:

var someObject = { str: "Some text", id: 5 };
console.log(someObject);

The output looks something like this:

[09:27:13.475] ({str:"Some text", id:5})

Outputting multiple objects

You can also output multiple objects by simply listing them when calling the logging method, like this:

var car = "Dodge Charger";
var someObject = {str:"Some text", id:5};
console.info("My first car was a", car, ". The object is: ", someObject);

This output will look like this:

[09:28:22.711] My first car was a Dodge Charger . The object is:  ({str:"Some text", id:5})

Using string substitutions

Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) introduced support for string substitutions. When passing a string to one of the console object's methods that accepts a string, you may use these substitution strings:

Substitution string Description
%o or %O Outputs a JavaScript object. Clicking the object name opens more information about it in the inspector.
%d or %i Outputs an integer. Number formatting is supported, for example  console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01
%s Outputs a string.
%f Outputs a floating-point value. Formatting is supported, for example  console.log("Foo %.2f", 1.1) will output the number to 2 decimal places: Foo 1.10

Each of these pulls the next argument after the format string off the parameter list. For example:

for (var i=0; i<5; i++) {console.log("Hello, %s. You've called me %d times.", "Bob", i+1);
}

The output looks like this:

[13:14:13.481] Hello, Bob. You've called me 1 times.
[13:14:13.483] Hello, Bob. You've called me 2 times.
[13:14:13.485] Hello, Bob. You've called me 3 times.
[13:14:13.487] Hello, Bob. You've called me 4 times.
[13:14:13.488] Hello, Bob. You've called me 5 times.

Styling console output

You can use the "%c" directive to apply a CSS style to console output:

console.log("This is %cMy stylish message", "color: yellow; font-style: italic; background-color: blue;padding: 2px");
The text before the directive will not be affected, but the text after the directive will be styled using the CSS declarations in the parameter.

Note: Quite a few CSS properties are supported by this styling; you should experiment and see which ones provide useful.

Using groups in the console

Requires Gecko 9.0(Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6)

You can use nested groups to help organize your output by visually combining related material. To create a new nested block, call console.group(). The console.groupCollapsed() method is similar, but creates the new block collapsed, requiring the use of a disclosure button to open it for reading.

Note: Collapsed groups are not supported yet in Gecko; the groupCollapsed() method is the same as group()at this time.

To exit the current group, simply call console.groupEnd(). For example, given this code:

console.log("This is the outer level");
console.group();
console.log("Level 2");
console.group();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.debug("Back to the outer level");

The output looks like this:

Timers

Requires Gecko 10.0(Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7)

In order to calculate the duration of a specific operation, Gecko 10 introduced the support of timers in the console object. To start a timer, call the console.time() method, giving it a name as the only parameter. To stop the timer, and to get the elapsed time in milliseconds, just call the console.timeEnd() method, again passing the timer's name as the parameter. Up to 10,000 timers can run simultaneously on a given page.

For example, given this code:

console.time("answer time");
alert("Click to continue");
console.timeEnd("answer time");

will log the time needed by the user to discard the alert box:

Notice that the timer's name is displayed both when the timer is started and when it's stopped.

Note: It's important to note that if you're using this to log the timing for network traffic, the timer will report the total time for the transaction, while the time listed in the network panel is just the amount of time required for the header. If you have response body logging enabled, the time listed for the response header and body combined should match what you see in the console output.

Stack traces

The console object also supports outputting a stack trace; this will show you the call path taken to reach the point at which you call console.trace(). Given code like this:

function foo() {function bar() {console.trace();}bar();
}foo();

The output in the console looks something like this:

Specifications

Specification Status Comment
Console API Living Standard Initial definition.

Notes

  • At least in Firefox, if a page defines a console object, that object overrides the one built into Firefox.
  • Prior to Gecko 12.0, the console object's methods only work when the Web Console is open. Starting with Gecko 12.0, output is cached until the Web Console is opened, then displayed at that time.
  • It's worth noting that the Firefox's built-in Console object is compatible with the one provided by Firebug.

See also

  • Tools
  • Web Console — how the Web Console in Firefox handles console API calls
  • Remote debugging — how to see console output when the debugging target is a mobile device
  • On-device console logging — how to do logging on Firefox OS devices

Other implementations

  • Google Chrome DevTools
  • Firebug
  • Internet Explorer
  • Safari

参考:

1. mozilla 文档: https://developer.mozilla.org/en-US/docs/Web/API/Console

2. google 文档: https://developers.google.com/chrome-developer-tools/docs/console-api

本文地址: http://blog.csdn.net/aerchi/article/details/69388863

[乐意黎翻译]JavaScript命令之Console大全相关推荐

  1. [乐意黎原创] JavaScript中数组使用总结

    原文地址:http://blog.csdn.net/u012468376/article/details/53147098 一. 数组的概念 1.1 什么是数组 数组是指的数据的有序列表. 数组中每个 ...

  2. [乐意黎转载]一个治愈 JavaScript 疲劳的学习计划

    像其他人一样,我最近偶然看到 Jose Aguinaga 的文章<在 2016 年学 JavaScript 是一种什么样的体验>". 译者注:中文翻译在此. 很显然,这篇文章触到 ...

  3. [乐意黎原创] WebPack 打包时抛Uncaught Error: Cannot find module '.\dist\bundle.js'

    如题:WebPack 打包时抛Uncaught Error: Cannot find module '.\dist\bundle.js' A. Webpack 命令时抛错 B.运行时Chrome 控制 ...

  4. [乐意黎原创] JS根据useAgent来判断edge, ie, firefox, chrome, opera, safari 等浏览器的类型及版本

    JS根据浏览器的useAgent来判断浏览器的类型. userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. javascript语法:navigator.us ...

  5. JavaScript新手入门教程大全~~~

    JavaScript新手入门教程大全~~~ 一. js教程介绍:JavaScript是一种运行在浏览器中的解释型的编程语言. 那么问题来了,为什么我们要学JavaScript?因为你没有选择.在Web ...

  6. javascript教程:console.log 详解

    对应WEB程序员,console.log 可以说是神器,极大地方便了程序开发.程序猿:学习了,用Console写日志比alert方便多了. console.log(object[, object, . ...

  7. JavaScript Debug 之 Console

    2019独角兽企业重金招聘Python工程师标准>>> 简评:只知道 console.log ?是时候提升一下对 console 的认知了. JavaScript console 是 ...

  8. [乐意黎原创]PHP抛PHP Startup:Unable to load dynamic library bcmath,Libmcrypt,mhash,mcrypt等警告及模块动态安装详解

    如下,Centos里启动 php-fpm 时,控制台总在抛若干警告. [root@aerchi] #service php-fpm startStarting php-fpm daemon is su ...

  9. [乐意黎]Centos里MySQL启动后无故停止,并抛 Warning: PDO::query(): MySQL server has gone away

    阿里云ECS里Centos自建 MySQL数据库,启动后经常无故停止服务. PHP程序有关MySQL的地方抛: Warning : PDO::query(): MySQL server has gon ...

最新文章

  1. 极速发展的饿了么订单系统架构演进
  2. css 相对单位rem详解
  3. 解决关于vs2010中w无法 显示的问题
  4. 02-Go语言数据类型与变量
  5. apache服务器安装
  6. boost升压斩波电路 分析
  7. zuc算法代码详解_ZUC祖冲之序列密码算法
  8. 关于SSL以及https的相关信息
  9. 关于收发邮件中的一些概念解释(收件人 抄送人 密送人 回复 回复全部)
  10. stm32看门狗定时器记录
  11. OpenJudge 海贼王之伟大航路
  12. ECharts学习--调色盘
  13. 2:STM32CubeMX配置STM32F103C8T6驱动-SPI驱动
  14. Mac自带的邮件 添加邮箱 无法验证账户或密码【已解决】
  15. 电商指标项目-背景及技术选型
  16. Aveva.Bocad.v2.1-ISO 1DVD(钢结构详图设计软件)
  17. html图片自动适应窗口大小,使用CSS自动调整浏览器大小的图片大小
  18. [Activeden] slick full website template with cms and 2 skins 中文版
  19. 在移动互联时代下,介绍几个Android 开发的新技术
  20. Markem imaje马肯依玛士喷码机维修9450E打码机维修

热门文章

  1. k8s 命令 重启_k8s常用命令
  2. 密码加盐(盐值salt)
  3. java 算数表达式 转成 二叉树,将算术表达式((a+b)+c*(d+e)+f)*(g+h)转化为二叉树。...
  4. MySQL练习题 答案和解析
  5. [附源码]计算机毕业设计springboot贷款申请审核管理系统论文
  6. 武汉市下吴地形图国家2000地方坐标转WGS84案例
  7. 【解决方案】电力巡检进入智能化时代,无人机+EasyDSS开启智能巡检新模式
  8. STM32串口通信代码正确串口却没反应
  9. ORCAD软件技巧【ORCAD,PADS,ALTIUM相互转换】[orcad关闭start page][OrCAD导入EDF元件原理图符号流程]
  10. 纯无趣技术贴,关于色深、位深、图像深度详解