[英]Why does console.log not require an argument in this case?
如果我按如下方式使用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);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.