语法错误:JavaScript 中的非法返回语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16068278/
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
Syntax error: Illegal return statement in JavaScript
提问by imulsion
I am getting a really weird JavaScript error when I run this code:
当我运行这段代码时,我遇到了一个非常奇怪的 JavaScript 错误:
<script type = 'text/javascript'>
var ask = confirm('".$message."');
if (ask == false)
{
return false;
}
else
{
return true;
}
</script>
In the JavaScript console it says:
在 JavaScript 控制台中,它说:
Syntax Error: Illegal return statement
Syntax Error: Illegal return statement
It occurs at return true;
and return false;
它发生在return true;
和return false;
(I am echoing this javascript from a php function; the $message
variable is one of the php parameters)
(我从一个 php 函数中回显了这个 javascript;该$message
变量是 php 参数之一)
What is wrong with my code?
我的代码有什么问题?
回答by Niet the Dark Absol
return
only makes sense inside a function. There is no function in your code.
return
只在函数内部有意义。您的代码中没有任何功能。
Also, your code is worthy if the Department of Redundancy Department. Assuming you move it to a proper function, this would be better:
还有,你的代码是否值得冗余部门。假设您将其移动到适当的功能,这会更好:
return confirm(".json_encode($message).");
EDIT much much later: Changed code to use json_encode
to ensure the message contents don't break just because of an apostrophe in the message.
很久以后编辑:更改了用于json_encode
确保消息内容不会因为消息中的撇号而中断的代码。
回答by prabeen giri
If you want to return some value then wrap your statement in function
如果你想返回一些值,那么将你的语句包装在函数中
function my_function(){
return my_thing;
}
Problem is with the statement on the 1st line if you are trying to use PHP
如果您尝试使用 PHP,则问题在于第一行的语句
var ask = confirm ('".$message."');
IF you are trying to use PHP you should use
如果您尝试使用 PHP,则应该使用
var ask = confirm (<?php echo "'".$message."'" ?>); //now message with be the javascript string!!
回答by Sunny S.M
in javascript return statement only used inside function block. if you try to use return statement inside independent if else block it trigger syntax error : Illegal return statement in JavaScript
在 javascript return 语句中只在功能块内使用。如果您尝试在独立的 if else 块中使用 return 语句,则会触发语法错误:JavaScript 中的非法 return 语句
Here is my example code to avoid such error :
这是我避免此类错误的示例代码:
<script type = 'text/javascript'>
(function(){
var ss= 'no';
if(getStatus(ss)){
alert('Status return true');
}else{
alert('Status return false');
}
function getStatus(ask){
if(ask=='yes')
{
return true;
}
else
{
return false;
}
}
})();
</script>
Please check Jsfiddleexample
请检查Jsfiddle示例
回答by Oleksandr Popugaiev
where are you trying to return the value? to console in dev tools is better for debugging
你想在哪里返回值?在开发工具中进行控制台更适合调试
<script type = 'text/javascript'>
var ask = confirm('".$message."');
function answer(){
if(ask==false){
return false;
} else {
return true;
}
}
console.log("ask : ", ask);
console.log("answer : ", answer());
</script>
回答by Oleksandr Popugaiev
In my experience, most often this error message means that you have put an accidental closing brace somewhere, leaving the rest of your statements outside the function.
根据我的经验,大多数情况下,此错误消息意味着您在某处意外放置了一个右大括号,而将其余语句留在了函数之外。
Example:
例子:
function a() {
if (global_block) //syntax error is actually here - missing opening brace
return;
} //this unintentionally ends the function
if (global_somethingelse) {
//Chrome will show the error occurring here,
//but actually the error is in the previous statement
return;
}
//do something
}
回答by Engineer
This can happen in ES6 if you use the incorrect (older) syntax for static methods:
如果您对静态方法使用不正确(较旧)的语法,则在 ES6 中可能会发生这种情况:
export default class MyClass
{
constructor()
{
...
}
myMethod()
{
...
}
}
MyClass.someEnum = {Red: 0, Green: 1, Blue: 2}; //works
MyClass.anotherMethod() //or
MyClass.anotherMethod = function()
{
return something; //doesn't work
}
Whereas the correct syntax is:
而正确的语法是:
export default class MyClass
{
constructor()
{
...
}
myMethod()
{
...
}
static anotherMethod()
{
return something; //works
}
}
MyClass.someEnum = {Red: 0, Green: 1, Blue: 2}; //works