在 JavaScript 中,哪些代码在运行时执行,哪些代码在解析时执行?

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

In JavaScript, what code executes at runtime and what code executes at parsetime?

javascriptruntime

提问by randomable

With objects especially, I don't understand what parts of the object run before initialization, what runs at initialization and what runs sometime after.

特别是对于对象,我不明白对象的哪些部分在初始化之前运行,哪些部分在初始化时运行,哪些部分在初始化之后运行。

EDIT: It seems that parsetime is the wrong word. I guess I should have formulated the question "In the 2-pass read, what gets read the first pass and what gets read the second pass?"

编辑:似乎 parsetime 是错误的词。我想我应该制定一个问题“在 2 遍阅读中,第一遍阅读什么,第二遍阅读什么?”

回答by Gareth

A javascript file is run in a 2-pass read. The first pass parses syntax and collects function definitions, and the second pass actually executes the code. This can be seen by noting that the following code works:

一个 javascript 文件以 2-pass 读取的方式运行。第一遍解析语法并收集函数定义,第二遍实际执行代码。通过注意以下代码可以看出这一点:

foo();

function foo() {
  return 5;
}

but the following doesn't

但以下没有

foo(); // ReferenceError: foo is not defined

foo = function() {
  return 5;
}

However, this isn't really useful to know, as there isn't any execution in the first pass. You can't make use of this feature to change your logic at all.

然而,这并不是真正有用的,因为在第一遍中没有任何执行。您根本无法利用此功能来更改您的逻辑。

回答by Shadow Wizard is Ear For You

Not sure what you ask exactly so I'll just share what I know.

不确定你问的是什么,所以我只会分享我所知道的。

JavaScript functions are "pre loaded" and stored in the browser's memory which means that when you have function declared in the very end of the page and code calling it in the very beginning, it will work.

JavaScript 函数被“预加载”并存储在浏览器的内存中,这意味着当您在页面的最后声明函数并在最开始调用它的代码时,它会起作用。

Note that global variables, meaning any variable assigned outside of a function, are not preloaded, so can be used only after being declared.

请注意,全局变量,即在函数外部分配的任何变量,未预加载,因此只能在声明后使用。

All commands outside of a function will be parsed in the order they appear.

函数之外的所有命令都将按照它们出现的顺序进行解析。

JavaScript doesn't really have "runtime", it can only respond to events or have code executed via global timers. Any other code will be parsed and "forgotten".

JavaScript 并没有真正的“运行时”,它只能响应事件或通过全局计时器执行代码。任何其他代码都将被解析并“忘记”。

回答by J?rg W Mittag

While JavaScript's direct ancestor is Scheme, JavaScript didn't inherit macros, so the answer is fairly simple: there is neverany code run during parse time.

虽然 JavaScript 的直接祖先是 Scheme,但 JavaScript 没有继承宏,所以答案相当简单:在解析期间永远不会运行任何代码。

回答by SLaks

Unlike C++, it is not possible to run logic in the Javascript parser.

与 C++ 不同,无法在 Javascript 解析器中运行逻辑。

I suspect that you're asking which code runs immediately and which code runs when you create each object instance.

我怀疑您在问哪些代码会立即运行,哪些代码会在您创建每个对象实例时运行。

The answer is that any code in a function that you call will only run when you call the function, whereas any code outside of a function will run immediately.

答案是,您调用的函数中的任何代码只会在您调用该函数时运行,而函数外的任何代码都会立即运行。

回答by masaya

Roughly speaking, Interpreter gets all variables and functions first, and then they get hoistedand executed.

粗略地说,Interpreter 首先获取所有变量和函数,然后将它们提升并执行。

For more detail, I hope these links might be helpful:

有关更多详细信息,我希望这些链接可能会有所帮助: