javascript Javascript中代码的执行顺序是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6776796/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:49:26  来源:igfitidea点击:

What is the order of execution of code in Javascript?

javascriptjavascript-eventsorder-of-execution

提问by Zeeno

How exactly is code in JavaScript executed? I mean in what order? Would there be a difference in the order of execution if I declared a function like this:

JavaScript 中的代码究竟是如何执行的?我的意思是按什么顺序?如果我声明一个这样的函数,执行顺序会不会有所不同:

function render() {
    // Code here
}

instead of this:

而不是这个:

var render = new function(){
    // Same code here
}    

Does JavaScript execute functions that are defined in a scripting file regardless of whether they're called by an event handler? (e.g. onload=function()).

JavaScript 是否执行脚本文件中定义的函数,而不管它们是否被事件处理程序调用?(例如onload=function())。

And finally if a function is defined in another function, when the parent function is called, is the lower function also called too? e.g.

最后,如果在另一个函数中定义了一个函数,那么在调用父函数时,是否也调用了下一个函数?例如

function a(){

    function b(){
        // code
    }

    function c(){
        //code
    }

}

I'm trying to get a concrete understanding of order of execution in JavaScript.

我试图具体了解 JavaScript 中的执行顺序。

采纳答案by Raynos

var render = new function(){
  // same code here
}

The newkeyword doesn't create a new Function. It creates a new object by running the function. So this would actually run the body of the method and return an object instead.

new关键字不创建一个新的功能。它通过运行该函数创建一个新对象。所以这实际上会运行方法的主体并返回一个对象。

If your asking when are functions parsedand added to scope then that's implementation specific, but all functions are hoisted to the top of scope and generally parsed before any code is executed.

如果您询问何时解析函数并将其添加到作用域,那么这是特定于实现的,但是所有函数都被提升到作用域的顶部,并且通常在执行任何代码之前进行解析。

Functions are only executedwhen you call them by invoking f()

函数 在您通过调用来调用它们时执行f()

回答by Quentin

A function declaration is hoisted (so it can be called earlier in the code then it is defined), a function statement isn't.

函数声明被提升(因此它可以在代码中更早地被调用然后被定义),函数语句不是。

Does JavaScript execute functions that are defined in a scripting file regardless of whether they're called by an event handler?

JavaScript 是否执行脚本文件中定义的函数,而不管它们是否被事件处理程序调用?

A function is called when it is called. Either because something has theFunctionfollowed by ()(possibly with arguments) or because it has been made an event handler.

一个函数在被调用时被调用。要么是因为theFunction后面跟着()(可能带有参数),要么是因为它已成为事件处理程序。

onload="function"

onload="function"

If that is JS, then it will assign a string to something expecting a function. If that is HTML, then you need ()to call the function.

如果那是 JS,那么它将为需要函数的东西分配一个字符串。如果那是 HTML,那么您需要()调用该函数。

And finally if a function is defined in another function, when the parent function is called, is the lower function also called too?

最后,如果在另一个函数中定义了一个函数,那么在调用父函数时,是否也调用了下一个函数?

No. A function is only called when it is called. Declaring a function inside another one just limits its scope.

不。一个函数只有在被调用时才会被调用。在另一个函数中声明一个函数只会限制它的作用域。

回答by Tomas Kohl

When you declare a function, it is not executed until it's called (that's true for ones declared in onload and other events as well).

当您声明一个函数时,它在被调用之前不会执行(对于在 onload 和其他事件中声明的函数也是如此)。

For nested functions, they are not executed automatically when the top-level function is called UNTIL the containing function calls them.

对于嵌套函数,当顶级函数被调用时它们不会自动执行,直到包含函数调用它们。