简体   繁体   English

为什么在这种情况下console.log不需要参数?

[英]Why does console.log not require an argument in this case?

If I use the Fetch API as follows: 如果我按如下方式使用Fetch API:

fetch(url)
    .then(response => response.json())
    .then(console.log)

I understand that this would log the result of the previous "then" (the response data), but why does console.log not require any arguments in this case? 我知道这将记录上一个“ then”(响应数据)的结果,但是为什么在这种情况下console.log不需要任何参数?

Is there any technical reasoning or documentation behind this, and can any other built-in methods be used in this nature? 其背后是否有任何技术推理或文档,并且可以以这种方式使用任何其他内置方法吗?

Thats simple Javascript, in this case the console.log requires that argument, what happens here is that the then function takes a callback as its first argument and internally executes that callback with the argument returned by the last then function. 多数民众赞成在简单的Javascript,在这种情况下, console.log需要该参数,这里发生的是then函数将一个回调作为其第一个参数,并使用最后一个then函数返回的参数在内部执行该回调。 So it means that you are passing the reference (or a copy, im not sure) of console.log function instead of directly executing this. 因此,这意味着您正在传递console.log函数的引用(或不确定的副本),而不是直接执行此操作。

In summary, this: 总而言之,这是:

function a (callback) {
  var something = 12345;
  callback(something);
}

a(console.log);

is the same as 是相同的

a(function(something) {
  console.log(something);
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM