在执行http.get请求时,为什么response.on(“data”,console.log)工作,response.on(“data”,myarray.push)不起作用?

在通过learnyounode进行工作时,我发现了一些我想从更有经验的开发人员那里获得一些洞察力的东西。

考虑一下:

var http = require("http") http.get(process.argv[2], (response) => { var collectData = [] response.on("data", collectData.push) // does not work response.on("data", console.log) // this works // this works and is what I am using right now response.on("data", (data) => { collectData.push(data) }) }).on("error", console.error) 

如您所见,发生response.on事件时,会将数据传递给console.log函数,而不是Array.push函数。