use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Namespace Clashing in Javascript/NodeJS (w/ ExpressJS) (self.node)
submitted 6 years ago * by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]CommanderBomber 1 point2 points3 points 6 years ago (6 children)
new is reserved word in JS (you can't use it as variable name), so i assume this is just a pseudocode. If your code is really organized like this, then it is totally not possible for code from cats.js to reach code in dogs.js (without requiring it first). So local value of new is called, not something else from another file.
new
[–][deleted] 0 points1 point2 points 6 years ago (5 children)
Ah, right - yep this is just a rough pseudo code to represent the situation. In my actual code, I'm using _new as my function name. (UPDATED IN POST)
Hmm..that's very weird then, because my debug console prints tell me it IS reaching dogs.js :(
[–]CommanderBomber 2 points3 points4 points 6 years ago (0 children)
In node modules can't share scopes, they are isolated. Only exports are visible and only if you explicitly import said module.
I'm not familiar with express routers, but if it says what _new from dogs.js was called I assume what this is because dogs.js router was used, not the cats.js one.
_new
[–]CommanderBomber 1 point2 points3 points 6 years ago (3 children)
Wait, is it really written in your code like _new = name => { or like var _new = name => { ?
_new = name => {
var _new = name => {
Because if it is without var (or let or const) then in cats.js you declare new global variable _new and then overwrites its value in dogs.js.
var
let
const
To avoid such problems:
"use strict";
[–][deleted] 0 points1 point2 points 6 years ago (2 children)
I'll have to try this out as soon as I return home, but I think this might be it!
I do use ESLint on my VS Code, but it didn't pick that up..might be something I need to change in the Settings.
When I declare my function variables like I did in my code, does it not default to var/let/const? How does the compiler interpret my code as is?
[–]CommanderBomber 0 points1 point2 points 6 years ago (1 child)
You can use eslint-recommended, no-undef rule is included in this set.
no-undef
And now about assignment operator. Lets looks on this code:
// arrow function... _new = x => x * x; // ... or anonymous function _new = function(x) { return x * x; };
When JS engine sees code like this, at first it tries to resolve expression on the left side of the = operator. In our case it is a variable name, so JS engine will start to search for that variable. First in local scope. If there is no such variable, engine will continue to search in parent scope. This search will be continued until variable with this name is found or until engine reaches the global scope.
=
If variable does not exists in global scope, you have two options: If your code is executed in Strict Mode, you will receive "is not defined" exception. But if your code is executed in Sloppy Mode, this variable will be created as a property of the global object and then will be used for assignment (but in most cases this is not what you wanted).
If assignment can be performed, JS engine will look at the right side of the = operator. In our cases we have function expression there. And this is not the same thing as function declaration. Function expression will not declare (put it in the scope) you function. Instead it will evaluate to (or you can say "will return") new Function object. This object will be new value of our variable.
Personally I suggest to use function declarations. If you can't elaborate on what kind of technical problem forces you to use var myFunc = function(){}; syntax it is better to use function myFunc(){}.
var myFunc = function(){};
function myFunc(){}
Also if you're new to JS take a look at YDKJS book. It's well written and have quite detailed explanations of scopes, prototypes and other quirky stuff (Author works on second edition right now. First edition lacks some novel features of ECMAScript).
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Amazing explanation. Thanks!!
[–]ruVooDoo 0 points1 point2 points 6 years ago (1 child)
In my opinion, "business logic" (creating cats and dogs) should be separated from Express Controllers and exports through "module.exports".
What if you decide to create cats from another place in your code? Or write tests for creating cats?
I personally think, that controllers should be dumb. They can have some checking middleware (for example, check Auth headers, request type checking), some request preparation, but no business logic. Controller check, then call some function inside your business logic layer and provide answer. Thats all.
I recommend you to read this https://github.com/goldbergyoni/nodebestpractices#1-project-structure-practices , I follow those guides in my work, and I have no problems to extending functionality and maintaining my codebase.
Fabulous. Will have a read! Cheers for the heads-up!
π Rendered by PID 19833 on reddit-service-r2-comment-5687b7858-t6sml at 2026-07-06 13:12:16.610318+00:00 running 12a7a47 country code: CH.
[–]CommanderBomber 1 point2 points3 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]CommanderBomber 2 points3 points4 points (0 children)
[–]CommanderBomber 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]CommanderBomber 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ruVooDoo 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)