解释我回调及其如何指代码中的数组

发布于 2025-01-23 21:49:55 字数 402 浏览 2 评论 0原文

我不明白此示例中的回调是什么 newarray.push(callback(this [i])); 当我得到它时(这个[i])是来自数组的项目,但是如何回调指代码;

const s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
  const newArray = [];
  
  for(let i=0;i<this.length;i++){
      newArray.push(callback(this[i]));
  }
  return newArray;
};



const new_s = s.myMap(function(item) {
  return item * 2;
});

console.log(new_s);

I dont understand what is callback in this example, espicially line
newArray.push(callback(this[i]));
as i got it (this[i]) is item from Array, but how does CALLBACK refer to code;

const s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
  const newArray = [];
  
  for(let i=0;i<this.length;i++){
      newArray.push(callback(this[i]));
  }
  return newArray;
};



const new_s = s.myMap(function(item) {
  return item * 2;
});

console.log(new_s);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2025-01-30 21:49:55

1。背景概念

首先,此顶部只是一个函数定义,其中'回调只是MyMap能够接受的参数。 “回调”一词并不特别,您实际上可以使用任何名称,但是“回调”确实表示您在功能定义中您要求呼叫者提供功能而不是整数或字符串。也就是说,您也可以写下这样的东西:

Array.prototype.myMap = function(param) {
   // you can console.log(param) and you would see the parameter in log.
}

从理论上讲,您可以通过执行来调用此功能:

s.myMap(1) // the log will show 1
s.myMap("hello") // the log will show "hello"
s.myMap(function() {}) // the log will show [Parameter is a Function]

其次,如果您将参数命名为“回调”,则可以向呼叫者发出信号,实际上他们可以将功能传递到此mymap中不仅是整数或字符串 - 因此,当您编写类似的内容时:

Array.prototype.myMap = function(callback_f) {
   callback_f(); // <----- call the incoming function passed as a param
}

呼叫者的想法是,他们必须以这种方式向mymap提供函数:

s.MyMap(function() {
   // do some stuff
})

或以这种方式:

function doStuff() {}
s.MyMap(doStuff)

无论哪种方式,参数 Callback_f 在这种情况下有望成为一个函数,而MyMap将调用并执行此功能,而不管您传递给它。

2。 回答您的问题

众所周知,

,这是一个特殊的函数定义,因为通过执行array.prototype.mymap,您正在修改所有阵列的工作方式,并且所有数组现在都将获得此函数定义mymap。其次,如果s是任何数组,则可以通过执行s.mymap()来调用此函数。

因此,在您的情况下,线:

newArray.push(callback(this[i]))

也可以写为:

let result_of_executing_the_callback = callback(this[i])
newArray.push(result_of_executing_the_callback)

这意味着:首先,在索引i处执行此(=当前数组)上的传入回调函数。传入的回调功能是什么?当您执行s.mymap(f)时,您要传递的功能 f

在您的情况下,f是:

function(item) {
  return item * 2;
}

1. Background concepts

Firstly, this top part is just a function definition where 'callback' is just a parameter that myMap is able to accept. The word 'callback' isn't special and you can use in fact any name, but 'callback' does signal that in your function definition you are asking the caller to supply a function instead of an integer or a string. That is, you could also write something like this:

Array.prototype.myMap = function(param) {
   // you can console.log(param) and you would see the parameter in log.
}

And in theory you could then call this function by doing:

s.myMap(1) // the log will show 1
s.myMap("hello") // the log will show "hello"
s.myMap(function() {}) // the log will show [Parameter is a Function]

Secondly, if you name your parameter 'callback', it signals to the caller that they could in fact pass a function into this myMap not just an integer or a string -- so when you write something like this:

Array.prototype.myMap = function(callback_f) {
   callback_f(); // <----- call the incoming function passed as a param
}

Then the caller has an idea that they have to supply a function into myMap, either in this way:

s.MyMap(function() {
   // do some stuff
})

Or in this way:

function doStuff() {}
s.MyMap(doStuff)

Either way, the parameter callback_f is expected to be a function in this case, and myMap will call and execute this function, regardless of what you pass into it.

2. Answering your question

As you may already know, this is a special function definition because, by doing Array.prototype.myMap you're modifying how all arrays work and all arrays will now gain this function definition myMap.

Secondly, you can call this function by doing s.myMap() if s is any array.

So in your case, the line:

newArray.push(callback(this[i]))

could also be written as:

let result_of_executing_the_callback = callback(this[i])
newArray.push(result_of_executing_the_callback)

which means: first, execute the incoming callback function on this (= current array) at the index i. And what is the incoming callback function? It is the function f that you are passing in when you do s.MyMap(f):

In your case f is this:

function(item) {
  return item * 2;
}

picture of f being passed into your function as the parameter 'callback'

(If this is helpful please mark as accepted!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文