Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7502837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:00:44+00:00 2026-05-29T21:00:44+00:00

I want to create modules to structure my NodeJS application, but I’m a little

  • 0

I want to create modules to structure my NodeJS application, but I’m a little lost, and I haven’t found anything (with hours of searching) that is completely definitive on the subject.

Say I’d like to create a “user” module, from which I can create new users in my code using something like:

var newUser = new User();

Ideally, I’d require my module at the top of my code using something like:

var User = require('../lib/user');

This works great. The question is, how should I structure the user module? Is the following the best way?

module.exports = function User()    {
    var authorized = false;
    var username = undefined;
    var password = undefined;
    var statistics = undefined;

    this.authorized = function()  {
        return authorized;
    }
    this.username = function()  {
        return username;
    }
    this.statistics = function()    {
        return statistics;
    }
}

I’m writing getters and setters for my various module variables, allowing me to hide things I don’t want to accidentally access from other code. However, I have done it this way before:

function User() {
    this.authStatus = false;
    this.email;
    this.displayName;
    this.inSession;
}

User.prototype.isAuthenticated = function() {
    return(this.authStatus && this.email && this.displayName)
}

User.prototype.isInSession = function() {
    return(this.inSession && this.isAuthenticated());
}

exports.User = User;

This works too, with one caveat; I haven’t found a way to access the user properties from within closures. If my understanding is correct, with the second implementation, I can’t. This means if I need to hand a function off to a db library as a callback to edit the user’s properties, I can’t. That’d look something like:

User.prototype.login = function()    {
    db.doDbStuff('get user data query', function(_error, _result)    {
         this.username = _result[0].name; //this code will not work
    });
}

The code doesn’t work, to my understanding, because the “this” keyword is within the scope of the closure, not the User. Even if the code were to be placed within the User function:

function User() {
    this.login = function()    { //you know

It wouldn’t work.

I guess my question is, what’s the best solution to this problem? Is it the method I presented in the first code block? That seems rather cumbersome and messy and prone to variable collision. I’m scared.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:00:47+00:00Added an answer on May 29, 2026 at 9:00 pm

    I typically go with the second approach, attaching functions to the prototypes.

    The issue you’re having with variables “not being available in closures” has nothing to do with prototypes. You’d have that same issue either way you structure it.

    It’s to do with javascript’s oft-confusing dynamic this:
    http://robotlolita.me/2011/10/09/understanding-javascript-oop.html#sec-2-1

    Basically, you need to do something like:

    User.prototype.login = function()    {
        var self = this // bind this to self so you have a reference to what `this` was
    
        db.doDbStuff('get user data query', function(_error, _result)    {
             self.username = _result[0].name; // self refers to the original `this`, so this works.
        });
    }
    

    You also have the option of using function.bind: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

    Within the bound function, the value of this will be whatever value you provided to .bind(value):

    User.prototype.login = function()    {
        db.doDbStuff('get user data query', (function(_error, _result)    { 
             this.username = _result[0].name; // bind fixes the original `this`, so this also works.
        }).bind(this));
    }
    

    Whether you use function.bind or self = this is somewhat of a personal taste question, but we were doing some benchmarks in the freenode#nodejs the other day and discovered bind() is 20 something times slower than var self = this.

    As to your original question about how to structure modules, there are so many examples to learn from on github. Simply find your favourite module and inspect how they structure it. I notice that many people seem to prefer factories over exposing constructors directly (e.g. require('module').create() ). Your call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application structure: /application /models ShoppingCart.php /modules /orders /models Order.php I want to create
I want to create a desktop application using SQLAlchemy and wxPython, but I'd like
I want to create a Java application bundle for Mac without using Mac. According
I think I want to use pythons built in calendar module to create an
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.