1

We can init function in such way

0[c="constructor"][c]`q=_=>1`()
q(); //1

As I take in it is the same as

Function`q=_=>1`() 

But I never saw such syntax.

Why and how does it work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kirill Matrosov
  • 5,564
  • 4
  • 28
  • 39
  • 1
    `0[c="constructor"][c]` is a short way of writing `(0).constructor.constructor`. Take a look at the values of `(0).constructor` and `(0).constructor.constructor` in your console. – Ry- Oct 24 '18 at 21:39
  • There's more to this than just [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). You also need to know what calling `Function` without `new` does and how arrays stringify. – melpomene Oct 24 '18 at 21:50
  • 1
    @melpomene `Function` without `new` works just like `Function` with `new`. And what does this have to do with arrays? – Bergi Oct 24 '18 at 21:54
  • 1
    @Bergi The code is equivalent to `Function(['q=_=>1'])()`. It passes an array to `Function`. – melpomene Oct 24 '18 at 21:56
  • @melpomene Oops, how could I forget that. But the duplicate seems to explain this well – Bergi Oct 24 '18 at 21:57

0 Answers0