javascript 声明两个同名变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23710231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring two variable with the same name
提问by Tukhsanov
采纳答案by The6P4C
In a code snippet such as yours, the variable a
is being redefined. This is because an if
statement doesn't create another scope for variables. However, functions do.
在像您这样的代码片段中,a
正在重新定义变量。这是因为if
语句不会为变量创建另一个作用域。但是,函数可以。
In a case like this:
在这样的情况下:
var a = 0; // global
function doStuff() {
var a = 10; // local
alert(a);
alert(window.a)
}
alert(a);
doStuff();
alert(a);
inside the function doStuff
, the variable a
is being redefined. This snipped will therefore alert the numbers 0
, 10
, 0
, 0
. This proves that the global variable is not redefined inside the function, as printing a
after calling doStuff
doesn't change the value of a
in the global scope.
在函数内部doStuff
,变量a
被重新定义。因此,此剪辑将提醒数字0
, 10
, 0
, 0
。这证明全局变量没有在函数内部重新定义,因为a
调用后打印doStuff
不会改变a
全局范围内的值。
The variable a
outside of the function can be accessed, as any variable not declared in a non-global scope is placed inside the window
object. However, if using this snippet (which calls an anonymous function, creating a new scope):
a
可以访问函数外部的变量,因为任何未在非全局范围内声明的变量都放置在window
对象内部。但是,如果使用此代码段(调用匿名函数,创建新范围):
var a = 0; // global
function doStuff() {
var a = 10; // local
alert(a);
alert(window.a)
function() {
var a = 20; // even more local
alert(a);
alert(window.a);
}();
}
alert(a);
doStuff();
alert(a);
you cannot access the value of a
inside the doStuff
function. You can still access the global variable using window.a
.
您无法访问函数a
内部的doStuff
值。您仍然可以使用window.a
.
In your case, however, the if
statement does not create a new scope, therefore you are redefining the variable a
to the new value $(window).height()
.
但是,在您的情况下,该if
语句不会创建新范围,因此您将变量重新定义a
为新值$(window).height()
。
回答by The6P4C
In this case, you have actually redefined the value of a
. There is absolutely no way of referencing a different variable with the same name, as it just acts as a redefinition.
在这种情况下,您实际上已经重新定义了 的值a
。绝对没有办法引用具有相同名称的不同变量,因为它只是作为重新定义。
回答by Pankaj
If you want to declare a global variable you can do so by
如果你想声明一个全局变量,你可以这样做
window.varname="This is a global variable";
And you can access the same by
你可以通过
alert(window.varname);
Now you can also have a local variable inside a function with the same name
现在您还可以在同名的函数中使用局部变量
var varname="This is a local variable";
And you can access it normally.
并且可以正常访问。
Here's your code so that you can access the global variable not the local one.
这是您的代码,以便您可以访问全局变量而不是本地变量。
var p = ['foo',''];
window.a = $(window).width();
if(!$.isFunction(p)){
var a = $(window).height();
alert(window.a);
}
回答by Chris Wesseling
There is no blockscope in JavaScript (at least up until ES6).
JavaScript 中没有块作用域(至少直到 ES6)。
Like you seem to expect from the if block. See What is the scope of variables in JavaScript?for an excellent summary of scopes that doexist in JavaScript.
就像您似乎对 if 块所期望的那样。请参阅 JavaScript 中变量的范围是什么?对JavaScript中确实存在的范围的一个很好的总结。
Beware of Hoisting
小心吊装
Furthermore, you shouldn't sprinkle your var
declarations through your code, but explicitly put them in the top of your function. That is where Javscript will hoistthem anyway:
此外,你不应该var
在你的代码中散布你的声明,而是将它们明确地放在你的函数的顶部。这就是 Javscript无论如何都会提升它们的地方:
# so if you have a function like this
var i = 5;
function testvar () {
alert(i);
var i=3;
}
testvar();
# the alert window will contain undefined.
# because internally, it's been changed into this:
var i = 5;
function testvar () {
var i;
alert(i);
i=3;
}
testvar();
Minimize use of the global scope
尽量减少对全局范围的使用
Read What is meant by “leaking” into global scope?
And listen to what Doug Crockford has to sayabout it. Actually, take an hour and watch the whole talk.
听听道格·克罗克福德 (Doug Crockford) 怎么说。实际上,花一个小时看整个演讲。
回答by Balachandran
Example:
例子:
var a=10;
if(true){
var a=5;
}
alert(a)// it will return a=5;
var a=10;
var a=5;
//both are same way assign value
In js if statement is not scope it visible every where with in function . you have to change the variable name
在 js 中,如果语句不在范围内,它在函数中的每个位置都可见。你必须改变变量名
回答by MasqueradeCircus
You can do it like this:
你可以这样做:
var p = ['foo',''];
var a = $(window).width(); // I want to call this variable
if(!$.isFunction(p)){
(function(b){
var a = $(window).height();
alert(b);
})(a);
}
No need to use the global scope, just create an anonymous function and call it with a
as the argument. Inside the function b
is a reference to the a
variable outside the function.
无需使用全局作用域,只需创建一个匿名函数并将其a
作为参数调用即可。函数内部是对函数外部变量b
的引用a
。
It is a good practice not to modify the window object in javascript to write clean and maintainable code.
最好不要在 javascript 中修改 window 对象以编写干净且可维护的代码。
Less bugs and problems. I mean, never do the window.a
thing. Is evil for your code.
更少的错误和问题。我的意思是,永远不要做那window.a
件事。对你的代码有害。
回答by guest271314
Try this (pattern)
试试这个(模式)
var p = ['foo', ''];
var a = function (name) {
return (
name === "height"
? $(window).height()
: (name === "width" ? $(window).width() : name)
)
};
if (!$.isFunction(p)) {
// `$(window).width()` , `$(window).height()`
alert( a("width") + "\n" + a("height") );
}
回答by TheGr8_Nik
No, you can't because of you have redefined the variable name in the same scope
and beacuse of the hoisted variables
your code will be interpreted by javascript in the following mode:
不,你不能,因为你已经重新定义了变量名same scope
,因为hoisted variables
你的代码将在以下模式下被 javascript 解释:
var p, a;
p = ['foo',''];
a = $(window).width(); // I want to call this variable
if(!$.isFunction(p)){
a = $(window).height(); // Not this one
alert(a);
}
Now you can easly see that the variable a
will be replaced and not created
现在您可以很容易地看到variable a
将被替换而不是被创建
回答by nettux
JavaScript has two scopes: global
and local
. In your example a
is in the global scope both times so you are just redefining it.
JavaScript 有两个作用域:global
和local
。在您的示例a
中,两次都在全局范围内,因此您只是在重新定义它。
However you can specify skip a variable in local
scope and get the one from global
. Consider this example:
但是,您可以指定在local
范围内跳过变量并从global
. 考虑这个例子:
var a = 1;
function foo () {
var a = 2;
console.log("var a is: " + window.a);
console.log("local var a is: " + a);
}
foo ();
Will log "var a is: 1"\n"local var a is: 2\n"
to the console. This is about as close as it gets to what you need
将登录"var a is: 1"\n"local var a is: 2\n"
到控制台。这与您所需要的差不多
回答by Rohit
var abc = new Array();
abc[0] = 'str1';
abc[1] = 'str2';
Use array in this case
在这种情况下使用数组