The Console.assert () function in JavaScript describes _javascript techniques

Source: Internet
Author: User
Tags array length assert

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.