Javascript 当被其他函数调用时,函数不进行控制台日志记录
我在一个Javascript 当被其他函数调用时,函数不进行控制台日志记录,javascript,Javascript,我在一个.js文件上有2个函数 function notUsed(id) { //default to false because if true then id not being used and good for new user var notInUse = false; console.log(notInUse); return !notInUse; } function generateID() {
.js
文件上有2个函数
function notUsed(id) {
//default to false because if true then id not being used and good for new user
var notInUse = false;
console.log(notInUse);
return !notInUse;
}
function generateID() {
//number of zeros represents the number of digits in id code
const SIZEOFID = 10000000;
const ID_DIGITS = 7;
//letter to start id for non los rios people
const STRTOFID = "C";
//variable to hold finished id code & variable to hold 7 digit of id code
var id, idNum;
//loop to make sure id contains 7 digits and 1 letter and not used already
do {
idNum = Math.round(Math.random() * SIZEOFID);
idNum.toString();
id = (STRTOFID + idNum);
}while(id.length != (ID_DIGITS+1) && notUsed(id));
console.log(id);
}
当我从网页调用generateID()
时,ID
会被记录,但false
不会被记录(显然notUsed函数不完整)。但是,如果我从网页中分别调用每个函数
,则ID
和false
都会被记录。我如何解决这个问题?任何注释都有帮助。逻辑and短路,因为第一次比较是错误的。第二个从未得到评估,这就是为什么它不记录日志。没有调用它。逻辑and正在短路,因为第一次比较是错误的。第二个从未得到评估,这就是为什么它不记录日志。它没有被调用。这是因为while id.length中的第一个条件!=(ID_数字+1)返回false,如果第一个条件返回false,则不会调用下一个条件
示例:
function imreturnTrue() {
console.log('imreturnTrue');
return true
};
function impreturnFalse() {
console.log('impreturnFalse');
return false
};
function imreturnTrue1() {
console.log('imreturnTrue1');
return true
};
let example = imreturnTrue() && impreturnFalse() && imreturnTrue1();
// imreturnTrue impreturnFalse
let example1 = imreturnTrue() && imreturnTrue1() && impreturnFalse() ;
// imreturnTrue imreturnTrue1 impreturnFalse
let example2 = impreturnFalse() && imreturnTrue() && imreturnTrue1() ;
// impreturnFalse
这是因为while id.length中的第一个条件!=(ID U DIGITS+1)返回false,如果第一个条件返回false,则不会调用next条件
示例:
function imreturnTrue() {
console.log('imreturnTrue');
return true
};
function impreturnFalse() {
console.log('impreturnFalse');
return false
};
function imreturnTrue1() {
console.log('imreturnTrue1');
return true
};
let example = imreturnTrue() && impreturnFalse() && imreturnTrue1();
// imreturnTrue impreturnFalse
let example1 = imreturnTrue() && imreturnTrue1() && impreturnFalse() ;
// imreturnTrue imreturnTrue1 impreturnFalse
let example2 = impreturnFalse() && imreturnTrue() && imreturnTrue1() ;
// impreturnFalse
它适用于me@Dij记录的ID只是一个带有单个字符和7的随机IDdigits@MarcoSalerno当从网页上的脚本标记内部调用generateID()时?显然未使用(id)不会执行。id.length!=(ID_DIGITS+1)返回false,因此不计算第二个操作数。我在堆栈溢出的代码段中尝试了它me@Dij记录的ID只是一个带有单个字符和7的随机IDdigits@MarcoSalerno当从网页上的脚本标记内部调用generateID()时?显然未使用(id)不会执行。id.length!=(ID_DIGITS+1)返回false,因此第二个操作数不会被计算我在堆栈溢出的snippetsHow中尝试了它。如果可能的话,我可以修复它吗?@NickPavini您可能想使用|
而不是&&
如果目的是验证生成的ID没有被其他用户使用,然后只需调用notUsed
。您编写的代码将保证idNum
的长度为8个字符,我认为您不需要进行检查。@johankarsson,但我需要这两种情况都为真。@NickPavini!(A&B)==!A | | |!B
如果可能的话,我如何解决这个问题?@NickPavini您可能想使用|
而不是&
如果目的是验证生成的ID没有被其他用户使用,那么只需调用notUsed
。您编写的代码将保证idNum
的长度为8个字符,我认为您不需要进行检查。@johankarsson,但我需要这两种情况都为真。@NickPavini!(A&B)==!A | | |!B