简体   繁体   English

语法错误:JavaScript 中的非法返回语句

[英]Syntax error: Illegal return statement in JavaScript

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

It occurs at return true;它发生在return true; and return false;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?我的代码有什么问题?

return only makes sense inside a function. return只在函数内部有意义。 There is no function in your code.您的代码中没有任何功能。

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以确保消息内容不会因为消息中的撇号而中断。

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!!

in javascript return statement only used inside function block.在 javascript return 语句中只在功能块内使用。 if you try to use return statement inside independent if else block it trigger syntax error : Illegal return statement in JavaScript如果您尝试在独立的 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 Jsfiddle example请检查Jsfiddle示例

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>

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
}

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

just i forgot to declare the word 'function' before the function.只是我忘了在函数之前声明“函数”这个词。 es es

myFunc(num)
{
   if(num > 0)
      return;
}

this produce 'illegal return statement' error, becouse miss 'function' before myFunc(num)这会产生“非法返回语句”错误,因为在 myFunc(num) 之前错过了“函数”

correct form:正确的形式:

function myFunc(num)
{
    if(num > 0)
       return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM