The Old “var”
JavaScript The Old “var” (JavaScript旧的“var”)
In this chapter, we represent the old scripts. It’s not about writing new codes. (在本章中,我们将介绍旧脚本。这与编写新代码无关。)
Let’s start at the very beginning. (讓 我們 從頭開始)
As you have already learned, there are three main ways for variable declaration:
const (CONST)
let (v.出租 ,允许 ,招租)
var (方差-)
The var originates from ancient times and is different from the other two. In general, it is not used in modern scripts, because variables declared with the var keyword have either global or function scope, but they do not exhibit block-level scope. Consequently, when a variable is declared within a loop or an if statement, it can be accessed and potentially redefined outside of that block, which may result in unintended behavior and a malfunctioning program. (VAR起源于古代,与其他两个不同。一般来说,它不用于现代脚本,因为使用var关键字声明的变量具有全局或函数范围,但它们不显示块级范围。因此,当在循环或if语句中声明变量时,可以访问该变量,并可能在该块之外重新定义该变量,这可能导致意外行为和程序故障。)
var may seem similar to let, as it can declare a variable. (var看起来可能类似于let ,因为它可以声明变量。)
For instance:
function welcomeSite() {
var siteMessage = "Welcome to w3cdoc"; // local variable, "var" instead of "let"
console.log(siteMessage); // Welcome to w3cdoc
}welcomeSite();
console.log(siteMessage); // Error, siteMessage is not defined
There are notable differences. (两者之间存在显著差异。)
First and foremost, the variables that var has declared are either global or function-wide. You can see them through blocks:
if (true) {
var answer = true; // use "var" instead of "let"
}
console.log(answer); // true, the variable lives after if
The code blocks are ignored by var. Hence there is a global variable answer. (代码块被var忽略。因此,有一个全局变量答案。)
If you apply let answer instead of the var answer, it can be possible to see the variable only inside the if. (如果应用let answer而不是var answer ,则可以仅在if中查看变量。)
For example:
if (true) {
let answer = true; // use "let"
}
console.log(answer); // Error: answer is not defined
The same is for loops:
for (var i = 0; i < 10; i++) {
// ...
}
console.log(i); // 10, "i" is a global variable, it's visible after loop
In case the code block is inside a function, the var transforms into a function-level variable, like this:
function welcomeSite() {
if (true) {
var siteMessage = "Welcome to w3cdoc";
}
console.log(siteMessage); // works
}
welcomeSite();
console.log(siteMessage); // Error: siteMessage is not defined
The var declarations are processed at the start of the function. So, it would be best if you define the var variables at the start of the function. (Var声明在函数开始时处理。因此,最好在函数开头定义var变量。)
It is visualized in the example below:
function welcomeSite() {
siteMessage = "Welcome to w3cdoc";
console.log(siteMessage);
var siteMessage;
}
welcomeSite();
It is possible to hoist declarations, but not the assignments. (可以提升声明,但不能提升分配。)
It is demonstrated in the example below:
function welcomeSite() {
console.log(siteMessage);
var siteMessage = "Welcome to w3cdoc";
}
welcomeSite();
The line var siteMessage = “Welcome to w3cdoc” consists of two actions:
Declare variable var (-声明变量var)
Assign variable =. (-赋值变量=。)
The declaration is processed at the beginning of the function execution. On the contrary, the assignment works at the place it comes out. The code works as follows:
function welcomeSite() {
var siteMessage; // declaration
console.log(siteMessage); // undefined
siteMessage = "Welcome to w3cdoc";
}
welcomeSite();
IIFE
IIFE (LIFE)
As initially existed only var having no block-level visibility, the developers created a way of emulating it. The invention was named “immediately-invoked function expressions” (IIFE). Of course, you shouldn’t use it at present but can find it in older scripts. It looks like this:
(function () {
let siteMessage = "Welcome to w3cdoc";
console.log(siteMessage); // Welcome to w3cdoc
})();
In the example above, a Function Expression is made immediately. Hence, the code invokes at once having its private variables. (在上面的示例中,立即生成一个函数表达式。因此,代码立即调用其私有变量。)
Summary
Summary (概要)
After learning what var is, you may have a better perception of old scripts in general. (在了解VAR是什么之后,您可能会更好地了解一般的旧脚本。)
There exist two significant differences of var compared with let and const:
There are not any block scopes for the var variables. They have minimum visibility at the function level. (- var变量没有任何块作用域。它们在功能级别的可见性最小。)
The declarations of var are processed at the beginning of the function. (- var的声明在函数开始时处理。)