简体   繁体   English

遍历一系列函数

[英]Iterating over an array of functions

I have the following JavaScript code: 我有以下JavaScript代码:

function f(){
}

var arrOfFuncs = [f, f, f, f];

for (var i in arrOfFuncs){
    console.log(typeof i);
    console.log(i);
}

When I run the script it prints: 当我运行脚本时,它会打印:

string
0
string
1
...
string
3

Since I am iterating over an array of functions, isn't "typeof i" supposed to be "function" and i.toString() supposed to be "function f(){}"? 由于我要遍历一组函数,因此“ typeof i”应该不是“ function”吗,i.toString()应该是“ function f(){}”吗?

No. The for…in construct iterates over the keys of an object or array, not its values . 否。for for…in构造遍历对象或数组的 ,而不是其

You can use a simple for loop: 您可以使用简单的for循环:

for (var i = 0; i < arrOfFuncs.length; i++){
    console.log(typeof arrOfFuncs[i]);
    console.log(arrOfFuncs[i]);
}

Or modify your for…in loop like this: 或像这样修改for…in循环:

for (var i in arrOfFuncs){
    console.log(typeof arrOfFuncs[i]);
    console.log(arrOfFuncs[i]);
}

Or you could use the forEach method (introduced in ECMAScript 5.1): 或者,您可以使用forEach方法(在ECMAScript 5.1中引入):

arrOfFuncs.forEach(function(f) {
    console.log(typeof f);
    console.log(f);
});

When you iterate over keys of an object with for(var in in ...) i will equal the key - in the case of an array you are getting the indices as well as properties of the array (0, 1, 2, length, etc) and the type of that index or property key might very well be a string. 当使用for(var in in ...)遍历对象的键时,我将等于键-对于数组,您将获得数组的索引和属性(0、1、2,长度等等),并且该索引或属性键的类型很可能是字符串。

You want to access the value of the array at that key, so you need to do arrOfFuncs[i] - that said, it is probably better to use either .forEach or some equivalent if you can or use 您想要访问该键处的数组的值,因此您需要执行arrOfFuncs[i] -也就是说,如果可以或使用,最好使用.forEach或等效的名称

for (var i = 0, l = arrOfFuncs.length; i < l; i++) {
    var value = arrayOfFuncs[i];
}

Because you avoid the risk of accessing properties on the array you don't intend to access. 因为避免了访问数组中的属性的风险,所以您不打算访问。

When you use for..in you are running over keys and not over values. 当您使用for..in时,您正在运行键而不是值。 Read about JavaScript for..in loops . 阅读有关JavaScript for..in循环的信息

This code will do what you want: 此代码将执行您想要的操作:

function f(){
}

var arrOfFuncs = [f, f, f, f];

for (var i in arrOfFuncs){
    console.log(typeof arrOfFuncs[i]);
    console.log(i);
}

JSFIDDLE JSFIDDLE

in your code i is the key it's not value. 在您的代码中,我是关键,它不重要。 so your code should be like this to get function type: 因此您的代码应像这样获取函数类型:

function f(){
}

var arrOfFuncs = [f, f, f, f];

for (var i in arrOfFuncs){
    console.log(typeof arrOfFuncs[i]);
    console.log(arrOfFuncs[i]);
}

in for in iteration over arrays, i doesn't represent the element, it represents the index. 在数组迭代中,我不代表元素,而是代表索引。

the below code works: 下面的代码有效:

var array = [function f(){}, function k(){}];

for (var i in array) {
    console.log(array[i]); //function()
    console.log(typeof array[i]); //function
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM