Introduction to the console. assert () function in JavaScript, console. assert

Source: Internet
Author: User

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 ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.