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

Javascript 在这种情况下,为什么console.log不需要参数?,javascript,es6-promise,fetch-api,Javascript,Es6 Promise,Fetch Api,如果我按如下方式使用Fetch API: fetch(url) .then(response => response.json()) .then(console.log) 我知道这将记录上一个“then”(响应数据)的结果,但是为什么console.log在这种情况下不需要任何参数 这背后是否有任何技术推理或文档,以及是否可以在这种情况下使用任何其他内置方法?这是简单的Javascript,在本例中,console.log需要该参数,这里发生的是then函数将回调作为其第

如果我按如下方式使用Fetch API:

fetch(url)
    .then(response => response.json())
    .then(console.log)
我知道这将记录上一个“then”(响应数据)的结果,但是为什么console.log在这种情况下不需要任何参数


这背后是否有任何技术推理或文档,以及是否可以在这种情况下使用任何其他内置方法?

这是简单的Javascript,在本例中,
console.log
需要该参数,这里发生的是
then
函数将回调作为其第一个参数,并使用最后一个
then
函数返回的参数在内部执行该回调。因此,这意味着您正在传递
console.log
函数的引用(或副本,我不确定),而不是直接执行此函数

总之,这:

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

a(console.log);

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

这是简单的Javascript,在本例中,
console.log
需要该参数,这里发生的是
then
函数将回调作为其第一个参数,并使用最后一个
then
函数返回的参数在内部执行该回调。因此,这意味着您正在传递
console.log
函数的引用(或副本,我不确定),而不是直接执行此函数

总之,这:

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

a(console.log);

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

由于
then
接受带有参数的回调,该参数是从上一个then返回的,因此该参数被传递到console.log。基本上
.then(function(json){console.log(json)})
.then(json=>console.log(json))
相同。then(thing=>console.log(thing))
,只是不创建匿名函数,所有参数都会为您传递。任何可调用的都可以这样使用。由于
then
接受一个回调,其参数是从上一个then返回的,因此该参数被传递到console.log。基本上
.then(function(json){console.log(json)})
.then(json=>console.log(json))
相同。then(thing=>console.log(thing))
,只是不创建匿名函数,所有参数都会为您传递。任何可调用的都可以这样使用。