11

While playing with the Developer Console in Firefox, I tried doing this:

var a = b => c => c;

and then this:

a(1)

I expected the result to be function() (corresponding to c => c), but this was displayed instead:

function a/<()

What is the meaning of this expression? It clearly isn't legal Javascript, since neither / nor < are valid characters for a function name.

The same happens using the regular notation for functions, i.e. var a = function(b) { return function(c) { return c; } }.

Here is a screenshot:

enter image description here

Edit: I tried the following

var a = b => c => d => d;
a(1)

and the result is

a/</<()

which makes me think it's some kind of lesser-known shorthand notation.

Giulio Muscarello
  • 1,312
  • 2
  • 12
  • 33
  • 2
    It returns `function c => c` just as it should in my browser (Chrome) ? – adeneo Dec 28 '15 at 19:08
  • @adeneo That makes it even weirder. – Giulio Muscarello Dec 28 '15 at 19:10
  • 2
    That's also specific to the built-in console in Firefox, Firebug is not affected. Intriguing question :) – Frédéric Hamidi Dec 28 '15 at 19:12
  • 1
    ...but `(b=>c=>c)(1)` gives the expected result... weird indeed. – spender Dec 28 '15 at 19:18
  • Does the same with the original function syntax too. Logging the result using `console.dir()`, we can expand the `.prototype` property and see that it has a `.constructor` with that same symbol. – Lye Fish Dec 28 '15 at 19:19
  • 2
    Don't forget that console output doesn't need to reflect any official language semantics. I wouldn't try to find too much meaning in this. Probably just a display bug. – Lye Fish Dec 28 '15 at 19:22
  • 3
    Forgive me if this seems trivial, but what exactly does `b => c` do? – Adjit Dec 28 '15 at 19:27
  • 1
    @Adjit: It's a new function syntax. `a = b => c => c` is similar but not identical to `a = function(b) { return function(c) { return c; }; };` – Lye Fish Dec 28 '15 at 19:29
  • 1
    @Adjit It's called the "fat arrow notation", introduced in ES5. `a => foo(bar)` is equivalent to `function(a) { return foo(bar) }`. – Giulio Muscarello Dec 28 '15 at 19:29
  • Interesting, thank you for the clarification. So the reason you would use this vs writing out a full function is just personal preference / trying to minimize file size? – Adjit Dec 28 '15 at 19:34
  • @Adjit: There are various differences. You can read about them [at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). It's more than just shorthand syntax. – Lye Fish Dec 28 '15 at 19:35
  • Is the version of Firefox you're using running ES6 yet? – jeffdill2 Dec 28 '15 at 19:36
  • @jeff, I'm running latest (43.0) and I can repro this. – Frédéric Hamidi Dec 28 '15 at 19:39
  • @FrédéricHamidi weird... – jeffdill2 Dec 28 '15 at 19:41
  • What's even more weird is that it actually functions properly - i.e. this `a()(1)` will correctly return 1. ¯\_(ツ)_/¯ – jeffdill2 Dec 28 '15 at 19:48
  • And if you do a `.toString()` on it, it will correctly show `"function (c) { return c }"`. – jeffdill2 Dec 28 '15 at 19:49
  • @jeffdill2 If my browser didn't support the ES6 fat arrow notation it'd have run into a syntax error. Indeed, my browser does support it, because `a()(1)` also works for me. – Giulio Muscarello Dec 28 '15 at 20:31
  • If you type function a() { return function(){}} and then a(1) it returns the same result a/<(). So it seems unrelated to the arrow notation. Appears to symbolize an anonymous function. Whatever the case, it's wasted a lot of my time. – Yogi Dec 28 '15 at 20:41

1 Answers1

3

Commenters on the relative issue on bugzilla have pointed out that it's part of a naming convention for anonymous function.

In particular,

  • a/b - inner b of var a = function() { var b = function() {}; }
  • a< - flags a "contributor" or basically some helper function which contributes to the function named a by being anonymous inside it.

So a/<() means that there is an anonymous function was declared in the body of a.

Giulio Muscarello
  • 1,312
  • 2
  • 12
  • 33