在 JavaScript 中,是否可以测试一个条件是否在程序的整个执行过程中保持为真?在这里,我想确保变量 a 从程序开始到结束始终可以被 3 整除。
//Assert that the following is always true, and print an error message if not true
ensureAlwaysTrue(a % 3 == 0); //print an error message if a is not divisible by 0
//from this point onward
a = 6;
a = 10 //print error message, since a % 3 != 0
function ensureAlwaysTrue(){
//this is the function that I'm trying to implement.
}
一种解决方案是在每个变量赋值后添加语句来检查断言,但这将是多余和麻烦的。是否有更简洁的方法来检查整个程序执行过程中条件是否为真?