简体   繁体   English

当console.log作为参数传递时,它起作用,但是当array.push被传递参数时,它不起作用,为什么?

[英]When console.log passed as a parameter it works, but when array.push is passed a parameter it doesn't work why?

I may be asking a silly question.but I am a beginner. 我可能在问一个愚蠢的问题。但我是初学者。

I saw this example in eloquent JavaScript in the chapter about higher order function. 在有关高阶函数的章节中,我用雄辩的JavaScript看到了这个示例。

There is a repeat function that takes 2 arguments. 有一个带有2个参数的重复函数。 1. number of time we want the repetition 2. action that we want to repeat 1.我们想要重复的次数2.我们想要重复的动作

When console.log is passed as 2 nd argument code works just fine. 当console.log作为第二个参数传递时,代码就可以正常工作。 it produces perfect output. 它产生完美的输出。

but when array.push is passed as 2 nd argument code throws an error, we are required to pass a function as 2 nd argument to make array.push work. 但是当array.push作为第二个参数代码引发错误传递时,我们需要传递一个函数作为第二个参数来使array.push工作。

Please help understanding this concept. 请帮助理解这个概念。

//code here works...
function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action(i);
  }
}

repeat(3, console.log);

//so why code below does not work...?
function repeat(n, action) {
    for (let i = 0; i < n; i++) {
      action(i);
    }
  }

let labels = [];
repeat(5, labels.push);

console.log(labels);

/*
In my understanding labels.push is also a function just like console.log, so it should push i 5 times(i.e 0-4) into labels array.
Please help me where am I wrong.
*/

//Why we require to pass a function as described below.
function repeat(n, action) {
    for (let i = 0; i < n; i++) {
      action(i);
    }
  }

let labels = [];
repeat(5, i => labels.push(i));
console.log(labels);

it is because labels.push requires a context. 这是因为labels.push需要上下文。

if you do repeat(5, labels.push.bind(labels)) , you will see that it works. 如果您repeat(5, labels.push.bind(labels)) ,您将看到它起作用。

i leave it to you read up on what fn.call(), fn.bind() and fn.apply() do. 我留给您阅读有关fn.call(),fn.bind()和fn.apply()的内容。

Doing repeat(5, labels.push.bind(labels)) works, but is pretty awful to read. 进行repeat(5, labels.push.bind(labels))可以,但是阅读起来很糟糕。 Use your arrow function i => labels.push(i) instead, because it's much easier to read. 请改用箭头功能i => labels.push(i) ,因为它易于阅读。

console.log is globally scoped, so function can access it. console.log是全局作用域的,因此函数可以访问它。 labels.push is out of repeat 's scope, so repeat is unable to access it. labels.push不在repeat的范围内,因此repeat无法访问它。 This page is very useful if you're trying to understand how arrow functions work 如果您想了解箭头功能的工作原理,则此页面非常有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将console.log作为参数传递给forEach时为什么不起作用? - Why doesn't console.log work when passed as a parameter to forEach? 为什么console.log()不对传递的变量进行快照? - Why doesn't console.log() take a snapshot of the passed variables? 为什么在异步代码上console.log()可以工作,而array.push()不能工作? - Why do console.log() works but not array.push() on async code? 为什么这样传递函数参数不成为键值? - Why doesn't function parameter become key value when passed in as such? 在传递给console.log()时,为什么在for循环中为数组分配值会导致该数组在每次迭代中具有相同的值? - Why does assigning a value to an array in a for loop result in the array having the same value for each iteration when passed to console.log()? Array.push()和console.log()提供不同的变量 - Array.push() and console.log() gives different variables 为什么 conv.add 在使用 API 时 console.log 不起作用 - Why doesn't conv.add work when console.log does when using an API 当console.log在函数中传递时出现非法调用错误 - Illegal Invocation error when console.log passed in a function 传递数组作为参数,不起作用 - Passed array as parameter and don't work 为什么console.log不能在JSC环境中工作,但它可以在Safari的调试控制台中运行 - Why doesn't console.log work in the JSC environment but it works in Safari's debug console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM