难以通过函数返回变量
我试图通过函数将变量返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有 3 个版本的“emailExists”变量:全局变量、checkExisting() 的参数以及 checkExisting() 中的本地变量!除了第一个之外,把所有的都去掉。另外,你永远不会调用 checkExisting()。
或者
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().
or
您应该调用 checkExisting 函数,而不需要从 var 到函数体中使用,因为它是在 page 上定义的。
You should call checkExisting function, and not needed use from
var
into body of function, because it's defined on page .简而言之...一切。
我想您是 javascript 和编程新手?您需要进行大量阅读才能了解对象作用域以及 javascript 的工作原理。我将快速浏览一下您所写的内容,以便您能够学到一些东西。
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.