0

I cam across this in the daggy code and was wondering what it meant. It resembles a tuple, but that's not supported

const typeRep = (0, (...args) => makeValue(fields, proto, args))

What is the purpose of the 0 in the first position of the parnes statement? I experimented with a couple simple examples

const t = (1, 10)
//=> 10

and

const t = (1, 10, 100)
//=> 100

As far as I can tell the expression throws away all but the last item in the parens. DOes anybody have the lowdown on what is happening here?

akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • 2
    Where exactly did you find this code? It looks pretty useless. – Bergi Apr 27 '17 at 16:55
  • If the people who voted to close can pinpoint where exactly the posted construction is being explained in that question, and specifically, _why_ it's being used like that. – robertklep Apr 27 '17 at 17:00
  • you are simple assigning, it will always take the value of the last regardless of what the first value is. const typeRep = (0, (...args) => makeValue(fields, proto, args)) is a method to add a lambda function to a var. – Edgar Apr 27 '17 at 17:05
  • 2
    Actually this question is duplicate of http://stackoverflow.com/questions/35522560/what-does-this-javascript-syntax-mean-0-parsekey2-defaultsomething and http://stackoverflow.com/questions/32275135/why-does-babel-rewrite-imported-function-call-to-0-fn I voted to reopen this question so we can mark it as duplicate of a thread that really answers the question (or is there a better way to do that?). – str Apr 27 '17 at 17:07
  • 1
    @str if you have enough rep, you can edit the _"This question has already an answer here:"_ block – robertklep Apr 27 '17 at 17:08
  • @robertklep I think the requirement is [to have a gold badge in one of the tags](https://meta.stackexchange.com/questions/291824/gold-tag-badge-holders-and-moderators-can-now-edit-duplicate-links)? – vaultah Apr 27 '17 at 17:16
  • 1
    @vaultah oh that could very well be. Anyway, I changed it :) – robertklep Apr 27 '17 at 17:19
  • @str No, it's not - there the comma operator is used because the second operand is a property access. Babel would not do this for a plain function expression. And here not even a call is involved. – Bergi Apr 27 '17 at 18:46
  • I fail to see how this is a duplicate or how the answer is already over there. This question is asking "what the heck does this even mean". While the linked answer explains how the comma operator works. The correct answer to this question would explain "Oh, that's the comma operator!" then explain how it works. Maybe I just care about actual semantics, but it really bugs me when things are closed in this manner. – Novaterata Apr 27 '17 at 18:53
  • @Bergi the code came from Daggy, https://github.com/fantasyland/daggy as I was looking into the "tagged" function – akaphenom Apr 28 '17 at 02:07

1 Answers1

0

The purpose of this comma operator, confusing as it is, is documented with a comment in the original source:

// this way we avoid named function
const typeRep = (0, (...args) => makeValue(fields, proto, args))

What does this mean? They don't want typeRep to implicitly get a .name = "typeRep" property, so they have explicitly avoided using a construct where an arrow function expression is directly assigned to an identifier.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375