0

I'm working with closures and using function properties. The code:

function sum(a) {

  let currentSum = a;

  function f(b) {
    currentSum += b;
    return f;
  }

  f.toString = function() {
    return currentSum;
  };

  return f;
}

console.log( sum(1)(2) ); // 3
console.log( sum(5)(-1)(2) ); // 6
console.log( sum(6)(-1)(-2)(-3) ); // 0
console.log( sum(0)(1)(2)(3)(4)(5) ); // 15

When I test this on the browser it works as expected, but in NodeJs I get:

[Function: f] { toString: [Function] }

Why does this happen?

  • 3
    "*When I test this on the browser it works as expected*" [I'm getting `function f(b)` in Firefox.](https://i.imgur.com/yYiHbEp.png) – VLAZ Apr 14 '20 at 18:31
  • @VLAZ The code seems to show the numbers (preceeded with f) in Chrome. – Teemu Apr 14 '20 at 18:35
  • 2
    use `console.log(sum(1)(2).toString())` in nodejs – namila007 Apr 14 '20 at 18:39
  • @Teemu hardly conclusive to what "as expected" would, though. Two out of three environments do not show this. Furthermore, there is really no standard when it comes to consoles - whether they execute `toString()` or not is not guaranteed in any way. Not between environments, not between consoles, not between different versions of the same environment or the same console. In fact, consoles *not* executing `toString()` should be the expected behaviour, after all when you do `console.log({ a: 1, b: 2 })`, you certainly don't expect to see `[object Object]` which is what `toString()` poroduces. – VLAZ Apr 14 '20 at 18:40
  • Saw that in IE in old days ... But you're right, consoles are not usually relying on `toString`. I'm bad with Chrome's console, but it shows the `f` as a "link" to the definition of function `f`, hence it looks like Chrome console shows also `toString` version ..? – Teemu Apr 14 '20 at 18:44
  • Yes, Chrome does. But again, that's *this* console in *this* version of Chrome. Without a standard, there is no way to guarantee that is a correct or expected behaviour. Since there is no guarantee, there is no way to make sure this happens or will continue happening. – VLAZ Apr 14 '20 at 18:54
  • 1
    @VLAZ I don't really know what the correct expected result is, But both chrome and nodejs are powered by the v8 engine, So I'd expect the same result. Now if the cause of the issue is the implementation of console.log on each environment is something to take care. –  Apr 14 '20 at 19:42
  • 1
    @limg21 yes, the consoles *are* different. The engine can work correctly as per spec (plus whatever the engine makers have added) but the consoles can absolutely show different things. For a start, in Chrome if you print an object, you can expand the properties and examine them as you wish. [This is done lazily in Chrome](https://stackoverflow.com/q/4057440/) through evaluating the reference *after* it was printed. In Node.js the console dumps to the stdout, so you can't do that thus at the very least, it serialises the object for display. Different behaviours - different consoles. – VLAZ Apr 14 '20 at 20:49

0 Answers0