I'm working through 'The CLI Book - Writing Successful Command Line Clients with Node.JS by Robert Kowalski' and he uses a pattern that I haven't seen before in Javascript.
I've got some code below, which uses '=>' as something like an assignment, but I cannot understand what it does. I haven't seen Javascript use different assignment approaches that way 'R' might for example '=' vs '<-'.
const glob = require('glob');
const path = require('path');
const files = glob.sync('*.js');
var arr = files.map(file => path.resolve(file));
console.log(arr);
The output is:
[ '/Users/jon/dev/js/a.js',
'/Users/jon/dev/js/glob.js',
'/Users/jon/dev/js/prime.js' ]
It seems that the file value is being declared and assigned in one, but if I modify the return statement to be the following:
var arr = files.map(file = path.resolve(file));
then it doesn't work and I get the following:
var arr = files.map(file = path.resolve(file));
^
ReferenceError: file is not defined
at Object.<anonymous> (/Users/jon/dev/js/a.js:4:41)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
I can't find reference to this '=>' operator anywhere in a bunch of books or on the web. He seems to use it in a number of places for example when defining a Promise:
function isOnline (url) {
return new Promise((resolve, reject) => {
request({
uri: url,
json: true
}, (err, res, body) => {
.
.
.
Any thoughts on what this is and why specifically this is being used instead of a normal assignment?
Jon