Javascript - 使用 for 循环声明变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13858128/
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
Javascript - Declare variables with a for loop
提问by JVG
I'm just wondering if it's possible to declare multiple variables using a for loop
in Javascript.
我只是想知道是否可以for loop
在 Javascript 中使用 a 声明多个变量。
Example:I have 4 <article>
elements whose Ids are article1
article2
article3
and article4
. Instead of declaring each variable independently, I'd like to do it in a loop (as they use essentially the same code and I don't want to repeat myself!).
示例:我有 4 个<article>
元素,其 ID 为article1
article2
article3
和article4
。我不想单独声明每个变量,而是希望在循环中进行(因为它们使用的代码基本上相同,我不想重复自己!)。
Something like:
就像是:
window.ready = function(){
for (var i=1; i<=4; i++){
var article[i] = document.getElementById("article" + i);
}
}
The above returns an error with an unexpected [
, so my syntax must be off (I'm very new to JS), but (a) is this on the right trackand (b) will these be global variables if I run them in a window.ready function?
以上返回了一个意外的错误[
,所以我的语法必须关闭(我对 JS 很陌生),但是(a)这是在正确的轨道上,(b)如果我在一个window.ready 函数?
回答by Felipe Oriani
You cannot set a variable name with [
char. Using it, you will get an exception of javascript. You can use a List/Array to do this, try with Array class, something like:
不能用[
字符设置变量名。使用它,您将获得 javascript 的例外。您可以使用列表/数组来执行此操作,尝试使用 Array 类,例如:
window.ready = function() {
var articles = new Array();
for (var i=1; i<=4; i++){
articles[i] = document.getElementById("article" + i);
}
}
With Array class, you are not required to specify the lenght, but if you want, you can set it on the constructor.
对于 Array 类,您不需要指定长度,但如果需要,您可以在构造函数中设置它。
var articles = new Array(4);
Another approach is use the push
method to add elements on the array, for sample:
另一种方法是使用该push
方法在数组上添加元素,例如:
window.ready = function() {
var articles = []; // define an empty array
for (var i=1; i<=4; i++){
articles.push(document.getElementById("article" + i));
}
}
回答by isNaN1247
Firstly its important to note that JavaScript functions have function scoperather than block scopemeaning that 'article' is not unique to your for loop. Therefore declaring var article
within your for
block may be misleading as to what actually happens at runtime.
首先重要的是要注意 JavaScript 函数具有函数作用域而不是块作用域,这意味着“文章”不是您的 for 循环所独有的。因此,var article
在for
块中声明可能会误导运行时实际发生的情况。
To achieve what you're after the following should work:
为了实现您的目标,以下应该有效:
window.ready = function(){
var article = [];
for (var i=1; i<=4; i++){
article.push(document.getElementById("article" + i));
}
}
References
参考
回答by t0.
You are creating a new Article Array everytime you loop through..
每次循环时,您都在创建一个新的文章数组。
Create an Article Array before entering your "for" loop.
在进入“for”循环之前创建一个文章数组。
article = new Array(4);
for (var i=1; i<=article.length; i++){
article[i] = document.getElementById("article" + i);
}
回答by freakish
You can try using eval
:
您可以尝试使用eval
:
for (var i = 1; i <= 4; i++) {
eval( 'var article'+i+' = document.getElementById("article' + i +'");' );
}
This will work like normal variables and they will be visible only in a local scope. This is probably a bad practice though, so just create an array or hash table to store dynamic variables.
这将像普通变量一样工作,并且它们仅在局部范围内可见。但这可能是一种不好的做法,因此只需创建一个数组或哈希表来存储动态变量。
回答by MrCode
Instead of that you could just grab all of the <article>
elements:
取而代之的是,您可以只获取所有<article>
元素:
var articles = document.getElementsByTagName("article");
// access them in a loop
for(var i=0; i<articles.length; i++)
{
console.log("Hey I'm " + articles[i].id);
}
回答by Sovan
Declare the array before using the array Ex : var article = []; // shorter version of new Array() then write your code as it is...
在使用数组之前声明数组 Ex : var article = []; // 较短版本的 new Array() 然后按原样编写代码...
Hope this will help
希望这会有所帮助