gpt4 book ai didi

javascript - TypeScript 变量范围与 JavaScript 变量范围

转载 作者:行者123 更新时间:2023-11-30 08:22:24 25 4
gpt4 key购买 nike

在这个非常常见的问题上,遇到了文档之间一些有趣的差异。

根据 TS 文档,用 var 声明的变量会从包含的函数中泄漏出来,但在 MSDN 上它声明 var 数据不会泄漏,而是可以访问的在整个包含函数中。与 ECMAScript 的处理方式相比,这可能只是 Typescript 处理 var 的方式的不同,期待 SO 对此的反馈。

According to the TS docs

Block-scoping When a variable is declared using let, it uses what some call lexical-scoping or block-scoping. Unlike variables declared with var whose scopes leak out to their containing function, block-scoped variables are not visible outside of their nearest containing block or for-loop.

But according to the MSDN docs

Variables declared by let have their scope in the block for which they are defined, as well as in any contained sub-blocks. In this way, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function:

最佳答案

这是表达同一事物的两种不同方式

文档说(强调我的):

variables declared with var whose scopes leak out to their containing function

这与说不同

variables declared with var whose scopes leak out of their containing function

关于javascript - TypeScript 变量范围与 JavaScript 变量范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51331418/

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