gpt4 book ai didi

javascript - 论点在词汇环境中的位置在哪里?

转载 作者:行者123 更新时间:2023-12-01 15:17:07 24 4
gpt4 key购买 nike

以下代码始终打印传入参数 a 的参数,无论是否存在同名变量。

大概是因为参数标识符单独绑定(bind)到范围内的变量。他们定位在哪里?他们在词汇环境中吗?

function foo(a, b = () => a) {
var a = 1
console.log(b())
}
foo() // undefined
foo(2) // 2


是吗 var声明以特殊 VariableEnvironment 结尾,而参数定位在 LexicalEnvironment 中?和 letconst通过使重新定义成为早期错误来避免冲突?

也相关:
  • 8.3.2 ResolveBinding(name [, env])
  • 8.1.1 Environment Records
  • 最佳答案

    如果 any default values are present ,为参数创建单独的环境记录。

    在这个位置声明的函数的语义使得这个环境记录定义了它们的本地范围。一个 note in the spec (见第 28 条)说:

    NOTE: A separate Environment Record is needed to ensure that closures created by expressions in the formal parameter list do not have visibility of declarations in the function body.



    更多来自 the spec :

    When an execution context is established for evaluating an ECMAScript function a new function Environment Record is created and bindings for each formal parameter are instantiated in that Environment Record. Each declaration in the function body is also instantiated. If the function's formal parameters do not include any default value initializers then the body declarations are instantiated in the same Environment Record as the parameters. If default value parameter initializers exist, a second Environment Record is created for the body declarations. Formal parameters and functions are initialized as part of FunctionDeclarationInstantiation. All other bindings are initialized during evaluation of the function body.



    因此,在没有默认参数的情况下,我推断预先存在的词法环境之一(VariableEnvironment 或 LexicalEnvironment)用于参数绑定(bind)。也许。

    关于javascript - 论点在词汇环境中的位置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61208843/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com