如何解决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];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。