ASSERT (Assert) is a good feature to ensure program correctness during the development and maintenance of JavaScript programs. On browsers with debugging tools, this feature can be implemented by calling Console.assert (). For example, in the following code, the Console.assert () statement guarantees that the score variable value of the Cat object has a length of 3:
Copy Code code 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.assert () statement, the first parameter is the result of an assert, which should normally be true, and the second parameter is an error message printed on the console when an error occurs. For example, the array length of the score variable in the above example is not 3 o'clock:
Copy Code code 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 executes, the Firebug console will print the error message:
Browser support
Console.assert () is supported on browsers with debugging tools, and is supported by major browsers. However, it is worth mentioning that Firefox itself does not support this feature, Firefox must be installed Firebug plug-ins to use Console.assert ().