Home > Web Front-end > JS Tutorial > body text

Detailed explanation on the usage of JavaScript built-in function console

巴扎黑
Release: 2017-08-10 13:49:10
Original
1689 people have browsed it

[Introduction] The console (Console) is the first panel of Firebug and the most important panel. Its main function is to display various information generated during the loading process of web pages. 1. The command to display information. Firebug has a built-in console object that provides 5 methods for displaying information. The simplest method is console l

               

Console.info("This is info");

Console.debug("This is debug");

Console.warn("This is a warn");

Console.error("This is an error");

Console.log("%d year %d month %d ",2011,3,26);

console.log("Pi is %f",3.1415926);

var dog = {};

dog. name = "大毛" ;

 dog.color = "yellow";

 console.log("%o",dog);

 console.group(" A set of information");

Console.log("The first item in the first group");

Console.log("The second item in the first group");

 console.groupEnd();

console.group("Second group information");

  console.log("Second group first item");

  console.log("Second group second item");

console.groupEnd();

dog.bark = function(){alert("woof woof woof"); };

console.dir(dog);

var table = document.getElementById("table1");

console.dirxml(table);

var result = 0;

console.assert( result );

var year = 2000;

console.assert(year == 2011 );

 function add(a,b){

  return a+b;

 }

 function add(a,b){

  console .trace();

Return a+b;

}

var x = add3(1,1);

function add3(a, b){return add2(a,b);}

 function add2(a,b){return add1(a,b);}

 function add1(a,b){return add(a,b);}

Console.time("Timer One");

for(var i=0;i

for(var j=0;j

 }

console.timeEnd("Timer One");

function Foo() {

  for(var i=0;i

  funcB(10000);

 }

Function funcA(count){

for(var i=0;i;i++){}

}

function funcB(count){

for(var i=0;i;i++){}

 }

console.profile('Performance Analyzer One');

Foo();

console.profileEnd();


The console (Console) is the first panel of Firebug and the most important panel. , its main function is to display various information generated during the loading process of web pages.

1. Commands to display information

Firebug has a built-in console object that provides 5 methods for displaying information.

The simplest method is console.log(), which can be used to replace alert() or document.write(). For example, if you use console.log("Hello World") in a web script, the console will automatically display the following content when loading.

Detailed explanation on the usage of JavaScript built-in function console

In addition, depending on the different nature of the information, the console object has four methods of displaying information, namely general information console.info() and debugging information console.debug (), warning prompt console.warn(), error prompt console.error().

For example, insert the following four lines into the web script:

 console.info("This is info");

 console.debug("This is debug");

 console.warn("This is warn");

 console.error("This is error");

When loading, the console will display the following content.

Detailed explanation on the usage of JavaScript built-in function console

You can see that information of different natures has different icons in front of it, and there is a hyperlink behind each piece of information. Click it to jump to the corresponding line of the web page source code.

2. Placeholders

The above five methods of the console object can all use printf style placeholders. However, there are relatively few types of placeholders, and only four types are supported: characters (%s), integers (%d or %i), floating point numbers (%f), and objects (%o).

for example,

Console.log("%d year %d month %d day", 2011,3,26);

 console.log("Pi is %f",3.1415926);

Detailed explanation on the usage of JavaScript built-in function console

%o placeholder can be used to view the internal conditions of an object. For example, there is such an object:

 var dog = {};

 dog.name = "大毛" ;

 dog.color = "yellow";

Then, use o% placeholder for it.

Console.log("%o",dog);

Detailed explanation on the usage of JavaScript built-in function console

3. Group display

If there is too much information, you can group it Display, the methods used are console.group() and console.groupEnd().

 console.group("The first group of information");

  console.log("The first item in the first group");

  console.log("First group, second item");

console.groupEnd();

 console.group("Second group of information");

  console.log("The first item in the second group");

  console.log("Second Group 2");

 console.groupEnd();

Detailed explanation on the usage of JavaScript built-in function console

Click the group title, and the group information will be collapsed or expanded.

Detailed explanation on the usage of JavaScript built-in function console

4. console.dir()

console.dir() can display all the properties and methods of an object.

For example, now add a bark() method to the dog object in Section 2.

 dog.bark = function(){alert("bark woof");};

Then, display the content of the object,

Console.dir(dog);

Detailed explanation on the usage of JavaScript built-in function console

5. console.dirxml()

console.dirxml() is used to display web pages The html/xml code contained in a certain node.

For example, first obtain a table node,

 var table = document.getElementById("table1");

Then, display the code contained in the node.

Console.dirxml(table);

Detailed explanation on the usage of JavaScript built-in function console

6. console.assert()

console.assert() is used to determine a Whether the expression or variable is true. If the result is no, a corresponding message is output to the console and an exception is thrown.

For example, the results of the following two judgments are both no.

 var result = 0;

 console.assert(result);

 var year = 2000;

 console.assert(year == 2011);

Detailed explanation on the usage of JavaScript built-in function console

##7. console.trace()

console.trace() to trace the function call trace.

For example, there is an adder function.

 function add(a,b){

Return a+b;

 }

I want to know how this function is called, just add the console.trace() method to it.

 function add(a,b){

  console.trace();

Return a+b;

 }

Assume that the calling code of this function is as follows:

 var x = add3(1,1);

 function add3(a,b){return add2(a,b);}

 function add2(a,b){return add1(a,b);}

 function add1(a,b){return add(a,b);}

After running, the call trace of add() will be displayed, from top to bottom, add() , add1(), add2(), add3().

Detailed explanation on the usage of JavaScript built-in function console

8. Timing function

console.time() and console.timeEnd() are used to display the running time of the code.

 console.time("Timer One");

 for(var i=0;i

 for(var j=0;j

 }

 console.timeEnd("Timer One");

Detailed explanation on the usage of JavaScript built-in function console

##9. Performance Analysis

Performance analysis (Profiler) is the analysis program The running time of each part to find out where the bottleneck is, the method used is console.profile().

Suppose there is a function Foo(), which calls two other functions funcA() and funcB(), of which funcA() is called 10 times and funcB() is called once.

 function Foo(){

 for(var i=0;i   funcB(10000);

 }

 function funcA(count){

 for(var i=0;i;i++){}

 }

 function funcB(count){

 for(var i=0;i;i++){}

 }

Then, you can analyze the running performance of Foo().

 console.profile('Performance Analyzer 1');

 Foo();

 console.profileEnd();

The console will display a performance analysis table, as shown below.

Detailed explanation on the usage of JavaScript built-in function console

#The title bar indicates that a total of 12 functions were run, taking a total of 2.656 milliseconds. Among them, funcA() runs 10 times, taking 1.391 milliseconds, the shortest running time is 0.123 milliseconds, the longest running time is 0.284 milliseconds, and the average is 0.139 milliseconds; funcB() runs once, taking 1.229ms.

In addition to using the console.profile() method, firebug also provides a "Profiler" button. When you click the button for the first time, "Performance Analysis" starts, and you can perform certain operations on the web page (such as ajax operations). Then when you click the button for the second time, "Performance Analysis" ends, and all operations triggered by this operation will be performed. Performance analysis.

Detailed explanation on the usage of JavaScript built-in function console

10. Properties Menu

After the name of the console panel, there is an inverted triangle. After clicking, the properties menu will be displayed.

Detailed explanation on the usage of JavaScript built-in function console

By default, the console only displays Javascript errors. If you select Send Javascript warnings, CSS errors, and XML errors, the relevant prompt information will be displayed.

The more useful thing here is "display XMLHttpRequests", which is to display ajax requests. After selecting, all ajax requests of the web page will be displayed in the console panel.

For example, if we click on a YUI example, the console will tell us that it issued a GET request using ajax. The header information and content body of the http request and response can also be seen.

Detailed explanation on the usage of JavaScript built-in function console

The above is the detailed content of Detailed explanation on the usage of JavaScript built-in function console. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template