![](/img/trans.png)
[英]Is there a way to return a boolean value through setInterval() function?
[英]Return boolean value through multiple functions
我有一些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; }
}
顯然,該示例沒有什么意義,但可以理解一般要點。 我需要做的是將功能three()
的true
(或false
)值返回到函數one()
,然后讓功能one()
將該值返回給任何稱為它的值。 我假設我必須返回到函數two()
以返回到一個,是否可以使用變量來做到這一點? 只是一個主意。 非常感謝您的幫助!
您可能要嘗試以下操作(如果我正確理解了您的問題):
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;
}
將調用更改為three()和two()以返回three()和返回two()。
var txt = 'foo';
function one(txt) {
return two(txt);
}
function two(txt) {
return three(txt);
}
function three(txt) {
return txt == 'foo'
}
嘗試:
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;
}
如果您喜歡三元運算符:
function one(txt) {
return (txt == 'foo') ? two(txt) : false;
}
function two(txt) {
return (txt == 'foo') ? three(txt) : false;
}
function three(txt) {
return (txt == 'foo');
}
您可以像上面所說的那樣進行操作,也可以在函數外部聲明一個變量,以便它是全局變量並僅引用它。 它不被認為是一種很好的做法,但是會起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.