📜 ⬆️ ⬇️

FireBug * Console API

Introduction


Firebug adds a global variable called “console” to each webpage loaded in Firefox. This object contains many methods that allow you to write to the Firebug console and display information passing through the scripts.
firebug.ru

Fortunately, not only firebug has this functionality:

This article is the Console API manual.
Examples are given from chrome Chrome 28.0.1500.72 m, firebug Firebug 1.11.3, firefox Firefox 22.0, opera Opera 12.15 (version before leaving presto)

Content




Manual


console.log (object [, object, ...])

chrome + firebug + firefox ± opera ±
Displays the arguments passed by the message to the console. If more than one argument is given, separates them with a space.
This is the most commonly used method of the console object.
Example
var a = ' '; console.log(a); console.log(' ', 12, true, [1,2,3], {a:1,b:2}); console.log('Node', document.getElementsByTagName('body')); console.log('DOM', document); console.log('', alert); console.log('', NaN, null, undefined); 
Screenshots
chrome Chrome -> firebug Firebug -> firefox Firefox -> opera Opera
console.log

In all the above console implementations, it is possible to click on a click to view the structure of complex elements, such as arrays, objects, DOM elements, etc.
')
There is a special case of using console.log with several arguments.
The first argument may contain substitution patterns as in printf () , but they are much smaller than in the implementation of this function in C, PHP etc.
chrome ± firebug ± firefox ± opera -
PatternDescription
% sLine.
% d or % iNumber.
% fFloating point number
Firebug's official documentation is blatantly lying "(numeric formatting is not yet supported)" - don’t believe it
% oDOM element
Firebug displays as a link to the item.
Chrome, in addition to the link, also knows how to display this element in the console itself. In the current version 28.0.1500.72 m there is a bug (?) - each time updating the page, the DOM console is displayed alternately as DOM, then as a Javascript object.
Firefox on click, opens the item in a modal window. (all attributes and methods of this object are available in it).
% OJavaScript object
Not supported in Firefox.
Chrome in the case when not just an object gets into% O, but DOM converts it into a js object (all methods and attributes are available).
Firebug does not distinguish% o from% O.
% ccss style (color, background, font).
Not supported in Firefox.
Example
 console.log('   %d %s',10,''); console.log('  %f',Math.PI); console.log('%c %c %c%c    ','color:red;','font-size:16px;color:orange;','background:black;color:yellow;','font:normal;color:normal;background:normal;'); console.log('body as DOM: %o',document.getElementsByTagName('body')[0]); console.log('object: %O',{a:1,b:2}); console.log('body as Object: %O',document.getElementsByTagName('body')[0]); 
Screenshots
console.log patterns

comment from streetstrider
The bottom line is that the console object can be implemented differently in different environments. In some cases, the object is implemented with full, albeit native functions, that is, if you try to display console.log.toString (); we get instead of the source code of the [native code] function. In some environments, as I understand it, this object simply provides things that can be called with parentheses, but they are not full functions: they cannot be called with call / apply / bind. From examples: Chrome debugger and IE debugger.
The main disadvantage:
Let there is an EventEmitter and we want to activate its activation. To do this, it is enough to sign some logger on it, for example console.log.
 object.on('event', console.log); 

That is, console.log will be called with all parameters with which any callback on this emitter is called. You can replace it with console.dir, then in some environments (Node.js) there will be a more detailed output of the first parameter. In any case, if console.log, console.dir are not functions, they cannot be called, you have to write a binding. Moreover, in the case of console.log, the binding will be incomplete: we will not be able to pass all the parameters, because we cannot make apply.
 function log (a1, a2, a3) { console.log(a1, a2, a3); //   arguments,     apply } 

And there is only one solution - without grace.
In general, functions must be functions.


console.assert (expression [, object, ...])

chrome + firebug + firefox - opera ±
If expression is wrong, outputs console.error ; otherwise, it does not output anything.
About differences throw new Error () from console.error () in more detail in console.error
If there are more arguments after the expression, then:

In Opera, if expression == false , it works like console.log , otherwise it doesn't output anything.
Example
 console.assert(2>1); console.assert(1>2); console.assert(2>1,' '); console.assert(1>2,' '); console.assert(1>2,'',''); console.assert(1>2,' : [%s]',''); console.assert(1>2,'','',1,2,3,{a:1}); 
Screenshots
console.assert


console.clear ()

chrome + firebug + firefox + opera +
Clears the console. Examples will not be.

console.count ([object])

chrome ± firebug + firefox - opera ±
Displays the number of times this code has been executed. If the optional object argument is passed, then the value of the counter is the output of object.toString () **.
All implementations have their own characteristics:
I do not think the implementation of Chrome is inaccurate, even if I put it ±. Just the look of google programmers on this feature is different from firebug programmers.
Example
 console.count(); console.count(); //    console.count(''); console.count(''); //      Chrome console.count('','',''); console.count(document); for (i=0;i<2;i++) console.count(); //      for (i=0;i<2;i++) console.count(''); //      
Screenshots
console.count


console.debug (object [, object, ...])

chrome ± firebug ± firefox ± opera ±
Alias ​​on console.log .
It exists for compatibility with the ancient version of the Console API, in which console.debug differed from console.log in that it added an additional link to the line in which the console call occurred. In the current API implementation, console.log does the same.
In Chrome - displays text in blue (text is displayed in dark gray in console.log ).
Example
 var a = ' '; console.debug(a); console.debug(' ', 12, true, [1,2,3], {a:1,b:2}); console.debug('Node', document.getElementsByTagName('body')); console.debug('DOM', document); console.debug('', alert); console.debug('', NaN, null, undefined); console.debug('   %d %s',10,''); 
Screenshots
console.debug


console.dir (object)

chrome ± firebug ± firefox ± opera +
Displays the passed value as a Javascript object (for DOM elements, all their attributes and methods are output).
Similarly, % O works in console.log in Chrome.
As in the case of console.count , everyone has their own approach to the implementation of this API method:
Example
 console.dir(' '); console.dir([1,2]); console.dir({a:1}); console.dir([1,2],{a:1}); console.dir(document); 
Screenshots
console.dir


console.dirxml (object)

chrome ± firebug ± firefox - opera ±
Displays the XML for the element.
Example
 console.dirxml(' '); console.dirxml([1,2]); console.dirxml({a:1}); console.dirxml(document); console.dirxml([1,2],{a:1}); 
Screenshots
console.dirxml


console.error (object)

chrome + firebug ± firefox ± opera ±
Displays the error and the result of console.trace for the place from which it was called.
! Patterns as in console.log are supported.
Implementation Differences:

! It is important to know:
Example
 console.error(); //   console.error('  '); console.error(' ', 12, true, [1,2,3], {a:1,b:2}); console.error('   %d %s',10,''); (function firstlevel () { (function secondlevel () { (function lastlevel () { console.error(' '); })(); })(); })(); console.error(new Error(' ')); //   
Screenshots
console.error


console.exception (error-object [, object, ...])

chrome - firebug ± firefox - opera ±
Displays the error and trace () result for the place from which it was called.
One of the most ambiguous methods:
Example
 console.exception(); //   console.exception('  '); console.exception(' ', 12, true, [1,2,3], {a:1,b:2}); console.exception('   %d %s',10,''); (function firstlevel () { (function secondlevel () { (function lastlevel () { console.exception(' '); })(); })(); })(); console.exception(new Error(' ')); //   
Screenshots
console.exception



console.group (object [, object, ...])

chrome + firebug + firefox ± opera +
Opens a new expanded group of entries in the console.
! Patterns are supported as in console.log
In Firefox, groups cannot be collapsed or expanded.

console.groupCollapsed (object [, object, ...])

chrome + firebug + firefox - opera +
Opens a new group immediately collapsed .
in Firefox - creates an expanded group.

console.groupEnd ()

chrome + firebug + firefox + opera +
Closes an open group.

Example
 console.log('  '); console.group(); //   console.log('   '); console.group(' ', '', '', '', 12, true, [1,2,3], {a:1,b:2}); console.log('   '); console.group(' %c', 'color: red;'); console.log('   '); console.groupCollapsed(' '); console.log('   '); console.groupEnd(); console.log('   '); console.groupEnd(); console.log('   '); 
Screenshots
console.group


console.info (object [, object, ...])

chrome ± firebug + firefox + opera +
Displays log with minor visual changes.
! Patterns are supported as in console.log
Example
 var a = ' '; console.info(a); console.info(' ', 12, true, [1,2,3], {a:1,b:2}); console.info('Node', document.getElementsByTagName('body')); console.info('DOM', document); console.info('', alert); console.info('', NaN, null, undefined); console.info('   %d %s',10,''); 
Screenshots
console.info



console.profile ([title])

chrome + firebug + firefox - opera -
Includes a Javascript profiler.
Code profiling helps you track which part of it takes the longest. Useful tool for debugging.

console.profileEnd ()

chrome + firebug + firefox - opera -
Turns off the Javascript profiler

Example
 console.profile(); (function someFunction() { for (i=0;i<10000000;i++) {var s='a'; s+='a'; delete s;} })() console.profileEnd(); console.profile(" "); (function someFunction() { for (i=0;i<10000000;i++) {var s='a'; s+='a'; delete s;} })() console.profileEnd(); 
Screenshots
console.profile


console.table (data [, columns])

chrome ± firebug + firefox - opera -
Displays a table.
In data , an array or an array-like object is expected.
The optional columns argument is used to set the names of the columns of the table, as well as for flexible control of the output of an array-like object.
(This functionality deserves a separate article. For details, go where the official documentation sends you.)
Chrome does not know how to display complex values, substituting for them such text as “Array [3]”, Object, etc.
Example
 var table = []; table[0] = [1,'2',[1,2,3]]; table[1] = [document,document.getElementsByTagName('body'),window]; table[2] = [{a:1},null,alert]; console.table(table); function Person(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } var family = {}; family.mother = new Person("", "", 32); family.father = new Person("", "", 33); family.daughter = new Person("", "", 5); family.son = new Person("", "", 8); console.table(family); 
Screenshots
console.table



console.time (name)

chrome ± firebug ± firefox ± opera ±
Creates a new timer and binds it to name . In this case, the name applies toString () **.


console.timeEnd (name)

chrome ± firebug ± firefox ± opera ±
Stops the timer associated with name.toString () and displays the elapsed time in the console thanks to mithgol for the correction .

console.timeStamp (name)

chrome - firebug ± firefox - opera -
Displays the current timestamp with the text that was passed to the name .
Opera throws an error if it hits a string with console.timeStamp () ; Chrome and Firefox - they are simply ignored.

Example
 console.timeStamp(); console.time(' '); (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.time('  '); (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd('  '); (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd(' '); console.timeStamp(' '); console.time(12); (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd(12); console.time(document.getElementsByTagName('script')); (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd(document.getElementsByTagName('body')); console.time(1,2,3); //   (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd(1,3,2); console.time(); //   (function someFunction() {for (i=0;i<1000000;i++) {var s='a'; s+='a'; delete s;}})() console.timeEnd(); console.timeStamp(' '); 
For Opera tests, the lines with timeStamp had to be commented out.
Screenshots
console.time


console.trace ()

chrome + firebug + firefox ± opera ±
Displays the call stack leading to the execution of this code (it is also displayed when error messages are reported).
Takes no arguments.

Example
 console.trace(); (function firstlevel () { (function secondlevel () { (function lastlevel () { console.trace(' '); })(); })(); })(); 

Screenshots
console.trace



console.warn (object [, object, ...])

chrome + firebug + firefox + opera +
Like console.info , it displays console.log with minor visual changes.
! Patterns are supported as in console.log
Example
 var a = ' '; console.info(a); console.info(' ', 12, true, [1,2,3], {a:1,b:2}); console.info('Node', document.getElementsByTagName('body')); console.info('DOM', document); console.info('', alert); console.info('', NaN, null, undefined); console.info('   %d %s',10,''); 
Screenshots
console.warn


Conclusion


Console is a very powerful and useful tool for web developers. I advise you to remember at least the main points:


useful links




* Rather, "Common Console API" because some methods support Firebug worse than Chrome.
** However, due to the toString method, it does not distinguish calls with document.getElementsByTagName ('script') argument from calls with document.getElementsByTagName ('body') , etc.

Source: https://habr.com/ru/post/188066/


All Articles