JavaScript是否在每次调用时编译该函数? [英] Does JavaScript compile the function every time it is invoked?

查看:105
本文介绍了JavaScript是否在每次调用时编译该函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个功能:

function A() {
  function B() {
    return 1;
  }

  return 1 + B();
}

每次我编译函数 B 时都会编译通话功能 A

Does function B gets compiled every time when I call function A?

我记得有人说它不会。但是我的记忆生锈了,我找不到引用。

I remember someones says it won't. But my memory gets rusty, and I cannot find the reference.

推荐答案

JavaScript标准规定JavaScript执行环境必须解析函数并产生早期错误,如果它们存在则拒绝执行脚本中的任何代码(例如缺少紧密引号,无法匹配的花括号等)。它没有说明之后会发生什么。

The JavaScript standard states that a JavaScript execution environment must parse the function and produce early errors refusing to execute any code in the script if they exist (such as missing close quotes, unmatched curly braces etc.). It says nothing about what happens after than.

但是,所有现代引擎都会在执行之前生成与该函数对应的本机代码。所有后续调用都将使用先前生成的代码。有时候引擎会使用通过执行收集的信息来重新生成代码,或者如果它可以确定这将导致更好的执行时间,它可能会内联函数的代码。

However, all modern engines will produce native code corresponding to the function prior to executing it. All subsequent invocations will use the code generated earlier. There are times where an engine will regenerate the code by using information it collected by execution or it might inline the code of the function if it can determine that would result in better execution time.

这篇关于JavaScript是否在每次调用时编译该函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆