javascript - console.log without arguments? How does this work? -
probably basic question haven't found answer in docs or google...
i doing nodeschool's "learnyounode" module, , intro http client question found official answer used console.log
without arguments (and indeed args in function, understand) :
var http = require('http') http.get(process.argv[2], function (response) { response.setencoding('utf8') response.on('data', console.log) response.on('error', console.error) })
how work? looks clean , obvious, i'm not sure can use style confidently without better understanding what's going on.
btw comparison (and see i'm not understanding), here's own similar, longer answer:
var http = require('http'); http.get(process.argv[2], function callback(response) { response.setencoding('utf8'); response.on('data', function(data) { console.log(data); }); response.on('error', function(error) { console.error(error); }); });
it's pretty simple @ code
response.on('error', function(error) { console.error(error); });
what did there wrote callback passed object error event can use error then.
now let @ other code
response.on('error', console.error)
the same happening event passes it's object callback function console.error
here takes 1 or more objects.
so like
let mylog = function(datatolog) { console.log(datatolog); } response.on('error', mylog);
would work, hope makes bit clearer.
Comments
Post a Comment