Javascript 使用 if 语句调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8387961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call function using if statement
提问by jcrowson
I want to be able to call a function within an if statement.
我希望能够在 if 语句中调用函数。
For example:
例如:
var photo = "yes";
if (photo=="yes") {
capturePhoto();
}
else {
//do nothing
};
This does nothing though. The function is clearly defined above this if statement.
但这没有任何作用。这个函数在这个 if 语句上面有明确的定义。
Edit: Wow, downboated to hell! capturePhoto(); was just an example function that didn't really need any more explanation in this scenario?
编辑:哇,下船了!捕获照片();在这种情况下只是一个不需要更多解释的示例函数吗?
回答by ComFreek
That should work. Maybe capturePhoto()
has a bug?
Insert an alert()
or console.log()
:
那应该工作。也许capturePhoto()
有一个错误?插入一个alert()
或console.log()
:
var photo = "yes";
if (photo == "yes") {
alert("Thank you StackOverflow, you're a very big gift for all programmers!");
capturePhoto();
} else {
alert("StackOverflow.com must help me!");
}
回答by TimWagaman
I'm not seeing any problems here. I used this code and the function call worked. I kept your code and just added a function called capturePhoto().
我在这里没有看到任何问题。我使用了这段代码并且函数调用起作用了。我保留了您的代码,只是添加了一个名为 capturePhoto() 的函数。
Are you sure that the code you're using to call the function is firing?
你确定你用来调用函数的代码正在触发吗?
var photo = "yes";
if (photo=="yes")
{
capturePhoto();
}
else
{
//do nothing
};
function capturePhoto()
{
alert("Pop up Message");
}
回答by Abdul Munim
You probably missed something, a quotation, a semicolon or something like that. I would recommend you to use a debugger like Firebugor even Google Chrome's Web Developer Tool. You will know what's wrong with your code and where it is wrong.
你可能错过了一些东西,一个引用,一个分号或类似的东西。我建议您使用Firebug 之类的调试器,甚至是 Google Chrome 的 Web Developer Tool。你会知道你的代码有什么问题以及哪里出错了。
You may take a look at this live code that your code above works: http://jsfiddle.net/ZHbqK/
您可以查看上面的代码有效的实时代码:http: //jsfiddle.net/ZHbqK/
回答by Joey Adams
The code looks fine to me (except you don't need the ;
at the end of the last line). Check your error log; perhaps the browser thinks capturePhoto
is not defined for some reason. You can also add alert
statements to make sure the code is actually running:
代码对我来说看起来不错(除非你不需要;
最后一行的末尾)。检查您的错误日志;也许浏览器出于某种原因认为capturePhoto
未定义。您还可以添加语句以确保代码实际运行:alert
var photo = "yes";
alert('Entering if statement');
if (photo=="yes") {
alert('then');
capturePhoto();
} else {
alert('else');
//do nothing
}
When you encounter a situation where it seems like a fundamental language feature is not working, get some more information about what is going on. It is almost never the platform's fault. It is occasionally a misunderstanding of how the feature works (e.g. why does parseInt('031') == 25
?). It is usually a violation of an assumption you're making about the code that isn't holding up because of a problem elsewhere.
当您遇到基本语言功能似乎不起作用的情况时,请获取有关正在发生的事情的更多信息。几乎从来都不是平台的错。偶尔会误解该功能的工作原理(例如,为什么parseInt('031') == 25
?)。这通常违反了您对由于其他地方的问题而无法阻止的代码所做的假设。
回答by Guffa
The code that you posted does work.
您发布的代码确实有效。
I copied it and tested it.
我复制了它并测试了它。
Demo: http://jsfiddle.net/Guffa/vraPQ/
演示:http: //jsfiddle.net/Guffa/vraPQ/
The only thing wrong with it that I can see is a semicolon after the closing bracket, but that is only a style problem. It will form an extra empty statement, but that doesn't cause any problems.
我能看到的唯一错误是右括号后的分号,但这只是样式问题。它会形成一个额外的空语句,但这不会导致任何问题。
回答by Smally
You should also consider using true
and false
instead of strings that could be manipulated depending on input.
您还应该考虑使用true
andfalse
代替可以根据输入进行操作的字符串。
If I had to correct the following code, then I should've done it like this;
如果我必须更正以下代码,那么我应该这样做;
var photo = true; // Will capture picture.
if (photo) { // 'true' is a truthy value.
capturePhoto();
} else {
// Do nothing
}