0

The following code will execute properly in the sandbox:

const concatR = function(arr, ...args) {
    let arr2 = (unnest = elem => !Array.isArray(elem) ? elem : elem.flatMap(unnest))(args);
    return arr.concat(arr2);
};
let x2 = concatR([1,2,3], 'hello', 'new', [1,2,3], [1,2,[3,4]]);
console.log(x2);

However, on my VS Code with Node.js v14.18.0, it gives an error:

ReferenceError: unnest is not defined

Is there a certain feature that (I think?) allows a variable definition to pass a named arrow function, or what feature exists in the code sandbox that isn't on my local machine? Locally, I have to separate the statements:

const concatR = function(arr, ...args) {
    const unnest = elem => !Array.isArray(elem) ? elem : elem.flatMap(unnest);
    let arr2 = unnest(args);
    return arr.concat(arr2);
};
let x2 = concatR([1,2,3], 'hello', 'new', [1,2,3], [1,2,[3,4]]);
console.log(x2);
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    You can fix the behaviour in the browser with `"use strict";`. – jonrsharpe Mar 23 '22 at 20:29
  • @jonrsharpe sure -- what feature is that blocking/enabling/changing though? – David542 Mar 24 '22 at 15:29
  • Search engine + "use strict" => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode, https://stackoverflow.com/q/1335851/3001761, ... – jonrsharpe Mar 24 '22 at 15:31
  • @jonrsharpe no I get that, I mean particularly the "named arrow on the rhs of a variable definition", or what actual feature is this blocking/not allowing. – David542 Mar 24 '22 at 15:36
  • 2
    This is not specific to arrow functions, anonymous or otherwise, or _"on the rhs"_. A minimal example is e.g. `unnest = "foo"`. You can search the MDN page for ReferenceError, which shows other examples. – jonrsharpe Mar 24 '22 at 15:42
  • @jonrsharpe ok got it, thanks for the example. – David542 Mar 24 '22 at 15:43

0 Answers0