Skip to content

1-js/06-advanced-functions/04-var#80

Merged
leviding merged 4 commits intojavascript-tutorial:zh-hansfrom
NoName4Me:translate
May 5, 2018
Merged

1-js/06-advanced-functions/04-var#80
leviding merged 4 commits intojavascript-tutorial:zh-hansfrom
NoName4Me:translate

Conversation

@NoName4Me
Copy link
Copy Markdown
Contributor

@NoName4Me NoName4Me commented May 3, 2018

resolve #71

@NoName4Me
Copy link
Copy Markdown
Contributor Author

@leviding 翻译完成~

@Starriers
Copy link
Copy Markdown
Contributor

@leviding 校对认领

Copy link
Copy Markdown
Contributor

@Starriers Starriers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoName4Me @leviding 校对完成

# 旧时的 "var"

In the very first chapter about [variables](info:variables), we mentioned three ways of variable declaration:
在前面的[变量](info:variables)那一章,我们讲到了变量声明的三种方式:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

讲到 =》 提到
在第一章关于变量的部分

如果我们在第二行使用 `let test`,那么 `alert` 就无法访问到它。因为 `var` 忽略块级代码,所以我们得到了一个全局的 `test`

The same thing for loops: `var` cannot be block- or loop-local:
循环也是这样,`var` 无法成为块级或循环局部:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var 在块级或局部循环中失效


*!*
alert(i); // 10, "i" is visible after loop, it's a global variable
alert(i); // 10, "i" 在循环结束后仍然可见,它是一个全局变量
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

它是 =》 会变成

`var` 声明在函数开始时处理(或者全局声明之于脚本开始)。

In other words, `var` variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
换言之,`var` 变量在函数开头被定义,无论定义在什么地方(假设定义不在嵌套函数里)。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无论定义在什么地方 =>与定义的位置无关

```

...Is technically the same as this (moved `var phrase` above):
...它完全等同于这个(`var phrase` 上提):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上提 =》上述文章提到的 最好扩展下

@NoName4Me
Copy link
Copy Markdown
Contributor Author

NoName4Me commented May 5, 2018

@Starriers 已修改,谢谢~ @leviding

@leviding leviding merged commit eb0d725 into javascript-tutorial:zh-hans May 5, 2018
@leviding
Copy link
Copy Markdown
Member

leviding commented May 5, 2018

很赞很标准

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants