难以通过函数返回变量

发布于 2024-11-05 12:12:47 字数 331 浏览 0 评论 0原文

我试图通过函数将变量返回 true 或 false。我想这样做,这样我就可以只调用 ajax 一次,并在一个函数中接收两个变量。但是,我在返回变量时遇到问题。这是我的代码:

var emailExists = false;
var userExists = false;

function checkExisting(emailExists,userExists) {
    var emailExists = true;
return emailExists;
}
alert(emailExists);

我不明白为什么警报给我假,而我认为它会给我真。这个设置有什么问题吗?

I'm trying to return variables as true or false via a function. I want to do it this way so I can call ajax only once, and receive both variables back in one function. However, I'm having trouble returning the variable. Here is my code:

var emailExists = false;
var userExists = false;

function checkExisting(emailExists,userExists) {
    var emailExists = true;
return emailExists;
}
alert(emailExists);

What I can't figure out is why the alert gives me false, when I thought it'd be giving me true. What's wrong with this setup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-11-12 12:12:47

您有 3 个版本的“emailExists”变量:全局变量、checkExisting() 的参数以及 checkExisting() 中的本地变量!除了第一个之外,把所有的都去掉。另外,你永远不会调用 checkExisting()。

var emailExists = false;

function checkExisting() {
    emailExists = true;
}
checkExisting();
alert(emailExists);

或者

var emailExists = false;

function checkExisting() {
    return true;
}
emailExists = checkExisting();
alert(emailExists);

You have 3 versions of the "emailExists" variable: the global one, the parameter to checkExisting(), and the local one in checkExisting()! Get rid of all but the first one. Also, you never call checkExisting().

var emailExists = false;

function checkExisting() {
    emailExists = true;
}
checkExisting();
alert(emailExists);

or

var emailExists = false;

function checkExisting() {
    return true;
}
emailExists = checkExisting();
alert(emailExists);
抚笙 2024-11-12 12:12:47
var emailExists = false;
var userExists = false;

function checkExisting(emailExists,userExists) {
    emailExists = true;
return emailExists;
}
checkExisting(false,true); //FOR EXAMPLE !
alert(emailExists);

您应该调用 checkExisting 函数,而不需要从 var 到函数体中使用,因为它是在 page 上定义的。

var emailExists = false;
var userExists = false;

function checkExisting(emailExists,userExists) {
    emailExists = true;
return emailExists;
}
checkExisting(false,true); //FOR EXAMPLE !
alert(emailExists);

You should call checkExisting function, and not needed use from var into body of function, because it's defined on page .

彼岸花似海 2024-11-12 12:12:47

简而言之...一切。

我想您是 javascript 和编程新手?您需要进行大量阅读才能了解对象作用域以及 javascript 的工作原理。我将快速浏览一下您所写的内容,以便您能够学到一些东西。

// Here you're declared two objects. 'emailExists' and 'userExists'.
// These Boolean objects, since they are not wrapped in a closure are now global
// (you can reference them anywhere) in your script.   
var emailExists = false;
var userExists = false;


// This function never gets called. If it did, it would always return true 
// since you have created a new 'emailExists' Boolean object in your function 
// and would return that each time.
function checkExisting(emailExists,userExists) {
    // This whilst only available within the function closure, is a no, no.
    // You're just confusing things by creating objects with the same name 
    // as global ones.
    var emailExists = true;

    // I'm returning true.
    return emailExists;
}

// Here you are returning your first declared Boolean (the one at the top)
// this will always return false.
alert(emailExists);

In short... everything.

I take it you are new to javascript and programming? You need to do a lot of reading so that you understand object scope and how javascript works. I'll give you a quick run-through of what you have written so you can hopefully learn something.

// Here you're declared two objects. 'emailExists' and 'userExists'.
// These Boolean objects, since they are not wrapped in a closure are now global
// (you can reference them anywhere) in your script.   
var emailExists = false;
var userExists = false;


// This function never gets called. If it did, it would always return true 
// since you have created a new 'emailExists' Boolean object in your function 
// and would return that each time.
function checkExisting(emailExists,userExists) {
    // This whilst only available within the function closure, is a no, no.
    // You're just confusing things by creating objects with the same name 
    // as global ones.
    var emailExists = true;

    // I'm returning true.
    return emailExists;
}

// Here you are returning your first declared Boolean (the one at the top)
// this will always return false.
alert(emailExists);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文