Introduction to the console. assert () function in JavaScript, console. assert
During the development and maintenance of JavaScript programs, Assert is a good feature to ensure program correctness. In browsers with debugging tools, this feature can be achieved by calling console. assert. For example, in the following code, the console. assert () Statement ensures that the score Variable Length of the cat object is 3:
Copy codeThe Code is as follows:
Function cat (name, age, score ){
This. name = name;
This. age = age;
This. score = score;
}
Var c = new cat ("miao", 2, [6, 8, 7]);
Console. assert (c. score. length = 3, "Assertion of score length failed ");
In the console. in the assert () Statement, the first parameter is the result of assert, which should be true under normal conditions. The second parameter is the error message printed on the console when an error occurs. For example, if the length of the score variable array is not 3 in the preceding example:
Copy codeThe Code is as follows:
Function cat (name, age, score ){
This. name = name;
This. age = age;
This. score = score;
}
Var c = new cat ("miao", 2, [6, 8]);
Console. assert (c. score. length = 3, "Assertion of score length failed ");
After the code is executed, the Firebug console prints the error message:
Browser support
Console. assert () provides better support for browsers with debugging tools. All major browsers support this function. However, Firefox does not support this function. You must install the Firebug plug-in on Firefox to use console. assert ().