javascript中的foreach函数

问题描述

| 好的,我的问题来自我试图理解的一本书中的示例。请记住,我刚开始使用javascript。 所以我们有了对象集,并定义了foreach函数。它使用另一个函数作为参数,并为属于set的数组“ values”的每个项目调用它。
set.foreach = function(f,c) {
    for(var i = 0; i < this.values.length; i++) 
        f.call(c,this.values[i]);
};
到目前为止,一切都很好.. 但是我不明白第二个代码片段中foreach函数用法,特别是我不理解变量v的作用。在书中其他地方都没有定义它,而且我很难了解它是如何工作的。我们在set中定义了另一个函数,以将值作为数组
set.toArray = function() {
    var a = [];
    this.foreach(function(v) { a.push(v); }); //where did v came from???
    return a;
}
    

解决方法

        
set.foreach = function(f,c) {
    for(var i = 0; i < this.values.length; i++) 
        f.call(c,this.values[i]);
}; //                 ^-------------- being passed right here
您传入的函数是
f
,并且调用ѭ3having时将其调用上下文的
this
值设置为
c
,并将
this.values[i]
作为第一个参数传递。
       // ------v---------your function \"f\" in the forEach
this.foreach(function(v) { a.push(v); });
       // ------------^------references the first argument (after the \"this\" arg)
       //                      that was passed to \"f\"
这是一个简单的示例: 该函数接受函数作为参数。它唯一要做的就是调用该函数:
function my_func( fn ) {
    fn();
}

   // call my_func,which will call the function you give it
my_func( function() { alert( \"hi\" ); } );
实时示例:http://jsfiddle.net/6a54b/1/ ...因此将函数传递到
my_func
会提醒字符串“ hi”。没有惊喜。 但是,如果“ 10”提供了要警告的值怎么办?
function my_func( fn ) {
    fn( \"message from my_func\" );  // call the fn passed,giving it an argument
}  //            ^------------------------------------------------|
   //                                                             |
   //               v------references the arg passed by my_func---|
my_func( function( arg ) { alert( arg ); } );
实时示例:http://jsfiddle.net/6a54b/ 现在您可以看到参数正在传递给我们要发送的函数,并且我们使用
arg
参数引用了该参数。 alerts10ѭ发出的任何声音都会发出警报。 我们甚至可以更进一步,通过向
my_func
传递第二个参数,即
my_func
将简单地接受并将其传递给我们传入的函数。
function my_func( fn,str ) {
    fn( str );  // call the fn passed,giving it
}                                  //    the string we passed in

   //               v------the arg we passed here-----v
my_func( function( arg ) { alert( arg ); },\"I\'m getting dizzy!\" );
实时示例:http://jsfiddle.net/6a54b/2/ 您会看到两个参数都被赋予了
my_func
,并且
my_func
调用了我们传入的函数,并向其传递了我们赋予它的字符串参数。     ,        变量“ 20”是传递给函数的参数。它使您可以处理函数收到的任何内容。在下面的示例中,它与“ 21”没有区别:
function sayHello(name) {
  console.log(\'Hello \'+name);
}
    ,        f.call表示以参数
this.values[i]
调用函数
f
。在foreach中设置了
this
。 从
toArray
调用
foreach
,通过function20ѭ传递函数,
values[i] in foreach
变成
v in toArray
。     ,        在这种情况下,将从调用语句
f.call(c,this.values[i])
传入
v
。详细地,是
this.values[i]
上面的语句等同于简单地:
f(this.values[i])
其中
f
是第二个片段中的功能(
function(v){ a.push(v); });
) 它被键入为
.call
而不是仅仅调用它的原因是为了可以设置
this
属性,因此第二个函数中的ѭ5is是数组本身,这意味着您可以键入:
function(v){
   alert(this.length); // \"this\" == array
}
    ,        很多答案,这是针对您问题的答案。
> this.foreach(function(v) { a.push(v); }); //where did v came from???
在传递给foreach的函数表达式中,v是形式参数。将标识符包含为形式参数或多或少等同于在函数主体中使用var声明它。也许写得更清楚:
this.foreach( function (v) {
    a.push(v);
});
或不...     ,        
v
代表列表中的每个项目,因为
foreach()
函数在它们之间循环。 因此,如果\ set中有10个项目,则该函数将被调用10次,将set中的每个项目作为参数ѭ20providing提供给函数 例如:
set = [1,2,3,4,5];

set.foreach = function(fn,context) {
  for(var i = 0; i < this.values.length; i++) {
    fn.call(context,this.values[i]);
  }
};

set_times_2 = [];
set.foreach(function(item) {
  set_times_2.push(item);
  set_times_2.push(item);
});

// this is true now:
set_times_2 == [1,1,5,5];