簡體   English   中英

變量是否可能獲得return的結果?

[英]Is it possible a variable get the result of return?

這是我的代碼;

var notificar = prompt("qual é seu nome ?");

function teste(name) {
    // return name +" Voce é o aluno";
    var teste =  return false;
}

if ( teste == false) {
    alert("Olá amiguinhos da tv (teste == false)");
} else {
    alert(" Ola amiguinhas da tv ??? (teste == true)");
}

alert(teste(notificar));

我想檢索一個布爾值返回值,以便可以執行條件結構

您需要像執行此操作一樣使用全局變量。 並且結構必須是正確的示例;

 var notificar = prompt("qual é seu nome ?"); // Declare global variable here var teste; function teste(name) { // return name +" Voce é o aluno"; // Set the global variable to (in this ase) false teste = false; } // First run the function to set the boolean teste(notificar) // Than do the check what teste is if (teste == false) { alert("Olá amiguinhos da tv "); } else { alert(" Ola amiguinhas da tv ???"); } 

在這行var teste = return false;

它將給出Uncaught SyntaxError: Unexpected token return錯誤。

即使您將其更改為return false; 以下代碼將不會執行,因為您從執行返回。

因此,我認為唯一的解決方案是將var teste分配為false

var teste = false;

實際上,如果您嘗試執行代碼,則會收到語法錯誤。 當您要檢索函數返回的值時,可以執行它並為變量分配屬性,如下所示:

var notificar = prompt("qual é seu nome ?");

function fnTeste(name) {
     return false;
}

var teste = fnTeste(notificar); // Here is the attribuition

if ( teste == false) {
    alert("Olá amiguinhos da tv ");
} else {
    alert(" Ola amiguinhas da tv ???");
}

alert(fnTeste(notificar));

請注意,變量“ teste”正在使用所需的函數“ fnTeste”的值(它們不能具有相同的名稱,因為它們用於不同的目的)。

希望對您有所幫助!

您可以使用'teste(notificar);'直接調用該函數。 並將結果存儲在聲明的“ var”中...

var notificar = prompt("qual é seu nome ?");
function teste(name) {
     // return name +" Voce é o aluno"; 
     return false; //returns the return value at calling function
}
var teste1=teste(notificar);
if ( teste1== false) {
     alert("Olá amiguinhos da tv (teste == false)");
}
else {
     alert(" Ola amiguinhas da tv ??? (teste == true)");
}
 alert(teste(notificar));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM