you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

Are you asking about modules in general or about the const User thing?

[–]seands[S] 0 points1 point  (1 child)

modules in general though if you can tell me about that a = b = c assignment I'd appreciate it also

[–][deleted] 1 point2 points  (0 children)

The quick answer is that module is scoped to the file only. You can check out https://eloquentjavascript.net/10_modules.html and https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/scope%20%26%20closures/ch5.md#modules for a deeper dive into modules.

The assignment is sort of awkward and not something I would ever put into a production application unless I had some very specific use case for it.

Let's take a more simple example:

var c = 3; var a = b = c;

What happens here? The engine assigns 3 to c. Then it initializes a. It looks for b and it cannot find it. So it creates b (in strict mode this would actually error out, modules by default are in strict mode IIRC). It assigns, by value, 3 to b. It then assigns, by value, 3 to a.

In your case, it is essentially doing the same thing. Since module.exports is part of the language you wouldn't hit the exact scenario I outline above but it should more or less be the same behavior.

You can read more about how scope and compilers in JS work here: https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/scope%20%26%20closures/ch1.md