An example of redeclaring a variable can be found in Google Analytics. When the JavaScript tracking code is initiated by the Google Analytics script, it declares or redeclares _gaq in this way:
var _gaq = _gaq || [];
In other words, if _gaq is already defined, _gaq is "redeclared" as itself. If it is not defined, it will be declared for the first time as an empty array.
This allows the Google Analytics tracking code to support other scripts which may need to use the variable before Google Analytics code has initiated. As @xralf pointed out, JavaScript allows for this.
Redeclaring a variable is useful in situations where it cannot be known if the variable has already been defined.
By redeclaring a variable conditionally, as Google Analytics tracking code does, it allows for a variable to safely originate from more than one place.
In this example it could be safe for other code using the _gaq variable to likewise check for a predefined _gaq variable. If it exists, it knows it can use it. If it doesn't exist, it knows that it should define it before trying to use it.
for (var x=0; x< 100; x++) { }
alert(x); //In most languages, x would be out of scope here.
//In javascript, x is still in scope.
//redeclaring a variable helps with clarification:
var x = "hello";
alert(x);
In javascript there is no block scope so it is advisable to redeclare a variable for clarification purposes; this makes for better code.
For example:
for (var x=0; x< 100; x++) { }
alert(x); //In most languages, x would be out of scope here.
//In javascript, x is still in scope.
//redeclaring a variable helps with clarification:
var x = "hello";
alert(x);
var x = 5;
var x;
// this is same as
var x; // undefined;
x = 5;
因此,当您说“如果您重新声明 JavaScript 变量,它不会丢失其值”。
根据提升,所有声明都会移至顶部。 然后变量被赋值。
var x = 25;
var x; // redeclare first time
var x; // redeclare second time
// is same as
var x; // undefined
var x; // Not sure if this happens, but doesn't make a difference, it's still undefined
x = 25;
var x = 5;
var x;
// this is same as
var x; // undefined;
x = 5;
So when you say "If you redeclare a JavaScript variable, it will not lose its value."
As per hoisting, the declaration(s), all of them , move to the top. And then the variable is assigned.
var x = 25;
var x; // redeclare first time
var x; // redeclare second time
// is same as
var x; // undefined
var x; // Not sure if this happens, but doesn't make a difference, it's still undefined
x = 25;
As for practicality, it happens sometimes. Look at @steveoliver 's answer.
Keep in mind that only variables declared with var can be re-declared. If you try to re-declare a variable declared with let or const (which is the ES2015 Javascript syntax that should be used in most cases nowadays), even worse than losing the value, an error will be thrown:
let foo = 'foo';
let foo;
So, in codebases which use modern Javascript syntax, re-declaring a variable simply isn't possible - the interpreter needs to be able to identify a single point in the code after which a let or const variable gets properly initialized. Before that point, the variable name will exist in the temporal dead zone.
一般来说,由于提升问题,在其他语句之后进行 var 赋值可能被认为是不好的风格 (参见此处)。使用“单变量模式”(参见此处),重新声明只能像 Steve Oliver 的 Google Analtyics 示例中那样发生。我将上面的示例重构为:
var x, max = 100; // no further var declarations afterwards!
for (x = 0; x < max; x++) { }
alert(x);
// redeclaration 'var x = "hello"' doesn't make any sense here
// and would be complained about by JSLint/-Hint
x = 'hello';
alert(x);
但是,当使用可选参数的默认值时,重新声明是有意义的(我认为这就是 Google Analytics 示例的内容):
function abc(param1) {
var param1 = param1 || 'default value';
}
In general, it can be considered bad style to have var assignments after other statements due to the problem of hoisting (see here). Using the "Single var pattern" (see here), redeclarations could only happen like in Steve Oliver's Google Analtyics example. I'd refactor the example above to:
var x, max = 100; // no further var declarations afterwards!
for (x = 0; x < max; x++) { }
alert(x);
// redeclaration 'var x = "hello"' doesn't make any sense here
// and would be complained about by JSLint/-Hint
x = 'hello';
alert(x);
A redeclaration can make sense however when using default values for optional parameters (which is what the Google Analytics example is about, I assume):
function abc(param1) {
var param1 = param1 || 'default value';
}
It is pretty simple re-declaring doesn't actually affect anything, you just have to remember that if you reassign value within scope then the reassigned value is limited to scope and outside of scope it will still be globally declared value
发布评论
评论(8)
这只不过是提醒您,如果您这样做:
结果将为 5。
例如,如果您在某些其他语言中重新声明变量 - 结果将是未定义的或 NaN,但在 javascript 中则不然。
It's nothing more than a reminder that if you do this:
Result will be 5.
If you re-declare variable in some other languages for example - result will be undefined, or NaN, but not in javascript.
可以在 Google Analytics 中找到重新声明变量的示例。当 Google Analytics 脚本启动 JavaScript 跟踪代码时,它会以这种方式声明或重新声明
_gaq
:换句话说,如果
_gaq
已定义,则_gaq
被“重新声明”为本身。如果没有定义,第一次会被声明为空数组。这允许 Google Analytics 跟踪代码支持其他可能需要在 Google Analytics 代码启动之前使用该变量的脚本。正如 @xralf 指出的,JavaScript 允许这样做。
在无法知道变量是否已定义的情况下,重新声明变量非常有用。
通过有条件地重新声明变量(如 Google Analytics 跟踪代码所做的那样),它允许变量安全地源自多个位置。
在此示例中,使用
_gaq
变量的其他代码同样检查预定义的_gaq
变量可能是安全的。如果存在,它就知道可以使用它。如果它不存在,它知道应该在尝试使用它之前定义它。An example of redeclaring a variable can be found in Google Analytics. When the JavaScript tracking code is initiated by the Google Analytics script, it declares or redeclares
_gaq
in this way:In other words, if
_gaq
is already defined,_gaq
is "redeclared" as itself. If it is not defined, it will be declared for the first time as an empty array.This allows the Google Analytics tracking code to support other scripts which may need to use the variable before Google Analytics code has initiated. As @xralf pointed out, JavaScript allows for this.
Redeclaring a variable is useful in situations where it cannot be known if the variable has already been defined.
By redeclaring a variable conditionally, as Google Analytics tracking code does, it allows for a variable to safely originate from more than one place.
In this example it could be safe for other code using the
_gaq
variable to likewise check for a predefined_gaq
variable. If it exists, it knows it can use it. If it doesn't exist, it knows that it should define it before trying to use it.你不应该。它会使代码变得混乱。
不。
You shouldn't. It makes for confusing code.
No.
在 javascript 中,没有块作用域,因此建议重新声明变量以进行澄清;这有助于编写更好的代码。
例如:
In javascript there is no block scope so it is advisable to redeclare a variable for clarification purposes; this makes for better code.
For example:
它不会因为提升
因此,当您说“如果您重新声明 JavaScript 变量,它不会丢失其值”。
根据提升,所有声明都会移至顶部。 然后变量被赋值。
至于实用性,有时会发生这种情况。看看@steveoliver 的回答。
It doesn't lose it's value because of Hoisting
So when you say "If you redeclare a JavaScript variable, it will not lose its value."
As per hoisting, the declaration(s), all of them , move to the top. And then the variable is assigned.
As for practicality, it happens sometimes. Look at @steveoliver 's answer.
请记住,只有使用
var
声明的变量才可以重新声明。如果你尝试重新声明一个用let
或const
声明的变量(这是当今大多数情况下应该使用的 ES2015 Javascript 语法),甚至比丢失值,将抛出错误:因此,在使用现代 Javascript 语法的代码库中,重新声明变量根本不可能 - 解释器需要能够识别代码中的单点,然后
let< /code> 或
const
变量已正确初始化。在此之前,变量名称将存在于临时死区中。Keep in mind that only variables declared with
var
can be re-declared. If you try to re-declare a variable declared withlet
orconst
(which is the ES2015 Javascript syntax that should be used in most cases nowadays), even worse than losing the value, an error will be thrown:So, in codebases which use modern Javascript syntax, re-declaring a variable simply isn't possible - the interpreter needs to be able to identify a single point in the code after which a
let
orconst
variable gets properly initialized. Before that point, the variable name will exist in the temporal dead zone.一般来说,由于提升问题,在其他语句之后进行
var
赋值可能被认为是不好的风格 (参见此处)。使用“单变量模式”(参见此处),重新声明只能像 Steve Oliver 的 Google Analtyics 示例中那样发生。我将上面的示例重构为:但是,当使用可选参数的默认值时,重新声明是有意义的(我认为这就是 Google Analytics 示例的内容):
In general, it can be considered bad style to have
var
assignments after other statements due to the problem of hoisting (see here). Using the "Single var pattern" (see here), redeclarations could only happen like in Steve Oliver's Google Analtyics example. I'd refactor the example above to:A redeclaration can make sense however when using default values for optional parameters (which is what the Google Analytics example is about, I assume):
这是非常简单的重新声明实际上不会影响任何东西,您只需要记住,如果您在范围内重新分配值,则重新分配的值仅限于范围,而在范围之外它仍然是全局声明的值
It is pretty simple re-declaring doesn't actually affect anything, you just have to remember that if you reassign value within scope then the reassigned value is limited to scope and outside of scope it will still be globally declared value