简体   繁体   English

为什么JavaScript在return语句之后允许函数声明?

[英]Why does JavaScript allow function declarations after the return statement?

JavaScript allows this: JavaScript允许这样做:

function outside() {
    inside();
    return 44;

    function inside() {
        console.log("inside");
    }
}

When outside is called the output is: 调用outside ,输出为:

inside
44

Why does this work and why doesn't it work for other statements like the one below: 为什么这样做有效,为什么对以下其他语句无效?

function outside() {
    return 44;
    console.log("inside");
}

which just prints 44 . 仅打印44

Because the file is parsed first and function definitions are read at that time. 因为首先分析文件,然后读取函数定义。 You are calling the function before you return , so that works just fine. 您在return之前就调用了该函数,因此可以正常工作。 Most programming languages allow you to define functions after calling them, because they all work in two steps: parsing and execution. 大多数编程语言都允许您在调用函数后定义函数,因为它们全部都分两个步骤工作:解析和执行。

What you see is the effect of hoisting . 您所看到的是提升的效果。 When a function is about to be executed, all the variable and function declarations are evaluated first, before the function is really executed. 当一个函数将要执行时,在真正执行该函数之前,首先评估所有变量和函数声明。 Thus your first function is equivalent to 因此,您的第一个功能等效于

function outside() {
    function inside() {
        console.log("inside");
    }
    inside();
    return 44;
}

Details can be found in the specification, 10.4.3 Entering Function Code and 10.5 Declaration Binding Instantiation . 可以在规范10.4.3输入功能代码10.5声明绑定实例中找到详细信息。

Why does JavaScript allow function declarations after the return statement? 为什么JavaScript在return语句之后允许函数声明?

Why the language is defined like this can probably only be answered by Brendan Eich. 为什么这样定义语言,只能由Brendan Eich回答。

In the first case first the function inside is executed and then it comes to return..so it returns 44.. 在第一种情况下,首先执行内部函数,然后返回return..so,则返回44。

But in the second case the outside function first encounters return which means to exit from that function no matter what is return below... so it only prints 44 但是在第二种情况下,外部函数首先遇到return,这意味着无论下面返回什么,都从该函数退出...因此,它仅输出44

function outside() {
inside(); //calling inside();
return 44;

function inside() {
    console.log("inside");
}
} 

but here 但在这儿

function outside() {
return 44;
console.log("inside");
}

you are just returning not calling inside() at all. 您只是返回根本不调用inside()

Function declaration: 函数声明:

function myName() {

}

Function Expression: 函数表达式:

var myName = function() {

};

These are very different, the function declaration (1) is defined when the Javascript is parsed, and not when it is executed. 它们是非常不同的,函数声明(1)是在解析Javascript时定义的,而不是在执行Java时定义的。 whereas the function expression (2) is defined when the Javascript is executed. 而函数表达式(2)是在执行Javascript时定义的。

So technically it is not being defined after the return statement. 因此从技术上讲,它不是在return语句之后定义的。

At least this is how I understand it. 至少我是这样理解的。

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

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