Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的foreach函数_Javascript - Fatal编程技术网

javascript中的foreach函数

javascript中的foreach函数,javascript,Javascript,好的,我的问题来自一本书中的一个例子,我正在努力理解它。请记住,我刚刚接触了javascript 我们有了对象集,定义了函数foreach。它将另一个函数作为参数,并为属于集合的数组“值”的每个项调用它 set.foreach = function(f,c) { for(var i = 0; i < this.values.length; i++) f.call(c,this.values[i]); }; v将列表中的每个项目表示为foreach()函数循环遍历

好的,我的问题来自一本书中的一个例子,我正在努力理解它。请记住,我刚刚接触了javascript

我们有了对象集,定义了函数foreach。它将另一个函数作为参数,并为属于集合的数组“值”的每个项调用它

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

v
将列表中的每个项目表示为
foreach()
函数循环遍历它们

因此,如果“set”中有10个项,函数将被调用10次,将集合中的每个项作为参数
v

例如:

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,2,2,3,3,4,4,5,5];
set=[1,2,3,4,5];
set.foreach=函数(fn,上下文){
对于(var i=0;i
变量
v
是传递给函数的参数。它允许您处理函数接收到的任何内容。它与以下示例中的
name
没有什么不同:

function sayHello(name) {
  console.log('Hello '+name);
}

f、 调用意味着使用参数调用函数
f
<代码>已设置foreach中的此。
关于从
toArray
调用
foreach
,用
v
传递函数,foreach中的
值[i]变为
toArray中的
v

v
在这种情况下是从call语句
f.call(c,this.values[i])
传入的。具体来说,它是
this.values[i]

上述陈述仅相当于:

f(this.values[i])
其中
f
是第二个代码段中的函数(
function(v){a.push(v);})

将其键入为
.call
而不是仅调用它的原因是,可以设置
this
属性,因此第二个函数中的
this
是数组本身,这意味着您可以键入:

function(v){
   alert(this.length); // "this" == array
}

下面是一个简单的例子:

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,2,2,3,3,4,4,5,5];
此函数接受函数作为参数。它唯一要做的就是调用函数:

function my_func( fn ) {
    fn();
}

   // call my_func, which will call the function you give it
my_func( function() { alert( "hi" ); } );
现场示例:

…因此,将函数传递给my_func将提醒字符串“hi”。不足为奇


但是如果
my_func
提供了要警报的值,该怎么办

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 ); } );
现场示例:

现在您可以看到一个参数正在传递给我们正在发送的函数,我们使用
arg
参数引用该参数

它会提醒
my_func
给它的任何东西


我们甚至可以更进一步,通过将第二个参数传递给
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!" );
现场示例:


您可以看到,这两个参数都被赋予
my_func
,并且
my_func
调用我们传入的函数,将我们赋予它的字符串参数传递给它。

很多答案,这里有一个是针对您的问题的

> this.foreach(function(v) { a.push(v); }); //where did v came from???
在传递给foreach的函数表达式中,v是一个形式参数。将标识符包含为形式参数或多或少等同于在函数体中用var声明它。如果写为:

this.foreach( function (v) {
    a.push(v);
});

或者不…

是,但外部函数根本不接受任何参数。内部函数怎么可能获取参数?@TAL“内部”函数由
foreach
函数提供参数-
foreach
是调用它的函数,并且是唯一可以为它提供参数的函数,而不是“外部”(
toArray
)函数。外部函数获取各种参数,集合本身被称为
。所以
setA.foreach(…)
不同于
setB.foreach(…)
,因为在
foreach
函数中,
这个
将引用
setA
setB
我更惊讶的是你没有问到函数名的缺失。参数是非常基本的,但是如果你已经做到了这一点,你在学习JS方面做得很好。这有点让人困惑。在
set.toArray
中,foreach的c参数没有传递任何值,因此当调用函数时,c将是未定义的,因此函数的this将设置为全局对象(在ES5 strict中除外,它将为
null
)。但这并不重要,因为函数没有使用它。这是一个非常低效的toArray函数,我想这只是一个例子。谢谢@alex。看来有人不同意你的意见o) @TAL:
arg
被定义为函数的参数。它的值由
my_func
传递给它的
my_func的消息
字符串指定。Patrick-“将c设置为调用上下文中的此值”不正确。call方法设置被调用函数的this值(即f的this,而不是foreach的this)。设置的不是c,而是f。无论如何,不会将任何值传递给f,这将解析为全局对象或null(取决于ES5 strict或其他代码)。@TAL:您必须认识到
函数是一个可以传递的对象。当我们将函数对象传递给
my_func
时,它可以调用该函数,并向其传递它想要的任何参数。然后,该函数将使用给定的任何参数(如果有)执行。@RobG:是的,这就是我所指的调用上下文。正在调用的
f
的上下文。我会澄清的。我可以说“设置其调用上下文”。
> this.foreach(function(v) { a.push(v); }); //where did v came from???
this.foreach( function (v) {
    a.push(v);
});