我有一些Javascript代码需要以返回的true或false值结束。但是,当计算true / false值时,原始值已经通过了多个函数,如下所示:
var txt = 'foo'
function one(txt) {
if(txt == 'foo') { two(txt); }
}
function two(txt) {
if(txt == 'foo') { three(txt); }
}
function three(txt) {
if(txt == 'foo') { return true; }
else { return false; }
}
显然,这个例子没有什么意义,但它得到了一般意义。我需要做的是将函数true
中的false
(或three()
)值一直返回到函数one()
,然后使函数{{1将该值返回到调用它的任何值。我假设我必须通过函数one()
返回到一个,有没有办法可以用变量做到这一点?只是一个想法。非常感谢您的帮助!
答案 0 :(得分:5)
您可能需要尝试以下操作(如果我理解您的问题):
function one(txt) {
if(txt == 'foo') return two(txt);
else return false;
}
function two(txt) {
if(txt == 'foo') return three(txt);
else return false;
}
function three(txt) {
if(txt == 'foo') return true;
else return false;
}
答案 1 :(得分:2)
将调用更改为三()和两()以返回三()并返回两()。
答案 2 :(得分:0)
尝试:
var txt = 'foo'
function one(txt) {
if(txt == 'foo') return two(txt);
else return false;
}
function two(txt) {
if(txt == 'foo') return three(txt);
else return false;
}
function three(txt) {
if(txt == 'foo') return true;
else return false;
}
答案 3 :(得分:0)
如果你喜欢三元运算符:
function one(txt) {
return (txt == 'foo') ? two(txt) : false;
}
function two(txt) {
return (txt == 'foo') ? three(txt) : false;
}
function three(txt) {
return (txt == 'foo');
}
答案 4 :(得分:0)
你可以像上面提到的人那样做,或者你可以在函数之外声明一个变量,所以它是全局的,只是引用它。它不被认为是很好的做法,但它会起作用。
答案 5 :(得分:0)
var txt = 'foo';
function one(txt) {
return two(txt);
}
function two(txt) {
return three(txt);
}
function three(txt) {
return txt == 'foo'
}