Javascript:打印出数组的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17098759/
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: print out index of an array
提问by hvu89
I have this question in an interview
我在面试中有这个问题
Using javascript, write an array of n elements. Each element in the array is a function that calls console.log of its own index. For eg, the first element will console.log 0, second element will console.log 1....
使用 javascript,编写一个包含 n 个元素的数组。数组中的每个元素都是一个调用自己索引的 console.log 的函数。例如,第一个元素是 console.log 0,第二个元素是 console.log 1....
Thanks in advance
提前致谢
Edit: Sorry for asking before researching. Im new to programming & dont know what closure is & couldnt find something in search. I got the answer myself but here is the problem I had. I wrote something like this
编辑:抱歉在研究之前询问。我是编程新手,不知道闭包是什么,在搜索中找不到东西。我自己得到了答案,但这是我遇到的问题。我写了这样的东西
n = 10
var array = []
for (var i=0; i < n; i ++) {
array[i] = function() {
console.log(i);
}
}
for (var j=0; j < n; j ++) {
array[j]()
}
回答by jfriend00
There isn't an easy way to do what you're asking because if you put a function in each array element, that function wouldn't know what it's array index was unless you somehow bound a custom function for each array index.
没有一种简单的方法可以完成您的要求,因为如果您在每个数组元素中放置一个函数,该函数将不知道它的数组索引是什么,除非您以某种方式为每个数组索引绑定了一个自定义函数。
But, all of this sounds a little like you're not telling us what the real problem is. If you're iterating over an array and calling a function in the array element, then just pass the index to that function as the first argument.
但是,所有这些听起来有点像您没有告诉我们真正的问题是什么。如果您要遍历数组并在数组元素中调用函数,则只需将索引作为第一个参数传递给该函数。
You could call each function in the array and pass it the index of its slot like this:
您可以调用数组中的每个函数并将其插槽的索引传递给它,如下所示:
function fn1(index) {
console.log(index);
// do other things here specific to this function
}
// fn2 ... fn4 definitions
var listOfFunctions = [fn1, fn2, fn3, fn4];
for (var i = 0; i < listOfFunctions.length; i++) {
listOfFunctions[i](i);
}
Or, tell us what the real problem is so we can more directly help solve that.
或者,告诉我们真正的问题是什么,以便我们可以更直接地帮助解决这个问题。
回答by basilikum
Here is one (of multiple ways) to do it.
这是一种(多种方式)来做到这一点。
var arr = [];
for (var i = 0; i < 10; i++) {
arr.push(function(){
console.log(this.indexOf(arguments.callee));
});
}
arr[3](); //3
arr[7](); //7
this
will refer to the array, if you call your function in the way arr[index]()
. arguments.callee
refers to the function itself, inside of itself. So the function searches itself inside of the array that it is attached to and prints out the found index.
this
将引用数组,如果您以方式调用您的函数arr[index]()
。arguments.callee
指的是函数本身,在其内部。因此该函数在它所连接的数组内部搜索自身并打印出找到的索引。
EDIT
编辑
As jfriend00 pointed out correnctly, arguments.callee
is deprecated and is not supported in strict mode.
So here is a "cleaner" solution (with suggestion from Bergi):
正如 jfriend00 正确指出的那样,arguments.callee
不推荐使用并且在严格模式下不受支持。所以这是一个“更清洁”的解决方案(来自贝尔吉的建议):
var arr = [], func;
for (var i = 0; i < 10; i++) {
arr.push(function printIndex(){
console.log(this.indexOf(printIndex));
});
}
arr[3](); //3
arr[7](); //7