Choosing between creating a class with a shared instance or not

I'm curious if my class is shared across instances: for example, I have a route that looks like this:

/student/:id

When this route triggers the controller (simplified version):

module.exports = RecalculateStudents;

const recalculateActiveStudents = require('../../DataModifier/ActiveStudents');

class StudentCalculator extends AbstractStudentController {    

    startApi(request, context) {

        //Are Students shared across calls or are they unique to each instance?
        let Students = recalculateActiveStudents.recalculateStudentTimes(ctx);

        //remainder of the code goes here
    }
}

module.exports = RecalculateStudents;

Then there's the RecalculateStudents class (used in the controller):

'use strict';

class StudentCalculator {

    constructor() {
        this.startPoint = null;
        this.finalProductStatus = null;
    }

    recalculateStudentTimes(ctx) {

        this.startGrades = ctx.currentGrades;
        this.finalStudentGrades = ctx.finalGrades;

        this.calculateActivities(this.startGrades)
    }

    calculateActivities() {        
        //
    }       
}

module.exports = new StudentCalculator();

I've omitted other methods from these classes for clarity.

My question is: In the controller, there's this line of code:

let Students = recalculateActiveStudents.recalculateStudentTimes(ctx);

And within that class, I have a constructor with two variables which I require. If another call is made, will Students be unique or shared among the calls?

I'm worried that if multiple calls (from different users) are made, those variables might get mixed up.

From what I understand, variables won't be shared as they are exported using the 'new' keyword.

Answer №1

The values of the variables will be completely unique in this situation. The use of this ensures that each reference is specific to the object utilizing it, without affecting any others (if I've interpreted your query correctly). If you do need to share a variable across multiple instances, employing the prototype would be necessary. It's important to export the class and create new instances, as opposed to exporting an individual instance of the class.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the recommended way to integrate Auth0 with a RESTful API?

Recently, I've been considering incorporating Auth0 for user authentication in my nodejs API. To add to the mix, I'm utilizing a MySQL database for user sign-ins and also want to implement Facebook login functionality. However, I've hit a r ...

Transforming SOAP into GraphQL

Is it possible for GraphQL to directly interact with a SOAP service as a data source, or is it necessary to create a SOAP-to-REST conversion layer? If such a conversion layer is needed, what technology (language, framework, OS, etc.) would be recommended f ...

What is the reason behind mongoose's utilization of schema, especially when MongoDB is known for its schema-less nature?

Just diving into the world of mongodb and feeling like a complete newbie. I've been utilizing mongoose to interact with mongodb through node.js, and while everything seems to be functioning properly, I can't help but wonder about the rationale be ...

Securely transmit data using a JQuery modal dialog form with HTTPS encryption

I currently have a functional modal login dialog. The only issue I'm facing is that when the original page is loaded through http, I still need to securely pass credentials to the server via https. Ideally, I would like to achieve this with minimal mo ...

Can a browser still execute AJAX even if the window.location is altered right away?

Here is the situation I am facing: <script> jQuery.ajax{ url : 'some serverside bookkeeping handler', type : post, data : ajaxData }; window.location = 'Some new URL'; </script> Scenario: I n ...

Tips on customizing image borders/masks with hover effects

Is there a React library or a simple CSS trick to create an image's canvas cropping effect on hover? For example, similar to this: Thanks in advance! ...

Exploring the use of Node.js exclusive modules in Docker containers

I have a nodejs project that includes references to a module I developed and hosted in a private Github repository. The dependencies listed in the package.json file are as follows: "dependencies": { ... other stuff ... "my_module": "git+https://gi ...

Upon completing the installation of Gulp, an error message stating "gulp command not found" may be displayed

Once I installed gulp.js using npm, an error message saying no command 'gulp' found popped up when trying to run the gulp command from the same directory it was installed in. Upon checking the node_modules/.bin/ directory, the gulp executable is ...

Unable to generate production package

NPM version (npm --v): 5.4.2 Gulp version (gulp -v): [email protected], [email protected] JHipster version: 4.6.1 Node version: 6.11.3 I am facing difficulties in generating a production package for my application. The process of running ...

What is the functionality of this JQuery Popup?

I'm facing an issue with my JQuery Popup. When the "Login" button is clicked, it hides the Login popup but doesn't show the Sign Up popup. How can I make sure that clicking on "Login" hides the Login popup and shows the Sign Up popup accordingly? ...

Unlock real-time alerts with the power of JavaScript and PHP!

I am currently working on enhancing my skills in javascript. I have a basic idea of what I want to achieve. My goal is to create an automated javascript function that accesses a php page. This php page will create an array of new notifications for the ja ...

Express throwing module errors

I encountered an issue while attempting to expose a REST service in an electron app using expressJS. Following a tutorial, I added express and @types/express to the project. However, when trying to implement a "get" method and running the build with ng bui ...

Mongoose encountering an issue with undefined properties (specifically 'prototype') and cannot read them

I am currently using Mongoose 7.0.1 on Next JS 13.2.2 and React 18.2.0. After tirelessly searching for a solution to my problem, I am still struggling with connecting to a MongoDB. Every time I try to import mongoose into my project for the connection, an ...

Creating packing features specifically designed for resolution within a reusable module

I've decided to revamp my Angular application based on John Papa's style guide (well, mostly) and my main focus is on improving modularity. The stumbling block I've encountered is with route resolves. So far, I've been using a global ap ...

Unable to Show the Contents of the Item - AngularJS

I'm in the process of developing an application that will showcase job details within a modal window based on the selected template. To achieve this, I've integrated ui.bootstrap and ui.router. However, I'm encountering difficulties in displ ...

Is there a way to update a variable in a controller using AJAX?

Is it possible to update a variable in the controller using Ajax? Controller: $basl = array(2018,11,18,0,0); $deger = 3; $baslamatarihi=Carbon::create($basl[0],$basl[1],$basl[2],$basl[3],$basl[4]); $bitistarihi = Carbon::create($basl[0],$basl[1],$basl[2] ...

Should one consider using Firestore for working with Node.JS in offline mode?

Recently, I developed a basic Node.js program on my Raspberry Pi 3 that interfaces with a Google Cloud Firestore database. My ultimate aim is to build iOS and Android apps that can access the Firestore database simultaneously. Upon testing going offline a ...

Comprehending the power of the mean stack while incorporating uglify.js and stylus technologies

As I delve into the world of the MEAN stack, I am aware that my question may seem basic to experts. Apologies in advance! Although I believe integrating Uglify.js and stylus would enhance the capabilities of this stack, I have been unsuccessful in doing s ...

The functionality of JQuery's .hover() is disabled once an HTML onClick() event is activated

I am currently working on a webpage and attempting to incorporate JQuery into it for the first time. However, I seem to be encountering some issues that I believe might have simple solutions. Background Information My JQuery code only contains one event l ...

Error Occurred while Uploading Images using Ajax HTML Editor with JSON Data

I am facing an issue with my AJAX HtmlEditorExtender, specifically when trying to upload an image. The error message I receive is as follows: JavaScript runtime error: Sys.ArgumentException: Cannot de-serialize. The data does not correspond to valid JSON. ...