exporting a freshly made object every time

I am trying to export a file that contains an object with a unique reference each time it is imported. Currently, when two files import this single export, they share the same reference and changes made in one file reflect in the other as well. I attempted using an IIFE, but it did not resolve the issue.

export.js

export const pagination =
    (function ()
    {
        let page_lengths =
            live
                ? [
                    5,
                    10,
                    15,
                    20,
                    25
                ]
                : [
                    1,
                    2,
                    3,
                    4,
                    5
                ]
        return {
            page:
                1,
            page_length:
                page_lengths[0],
            page_lengths,
            total:
                0,
        }
    })()

import.js

import {
    pagination,
} from "export.js"

Answer №1

Alternatively, you can delay the invocation:

export.js

export const pagination = 
(function ()
{
    let page_lengths =
        live
            ? [
                5,
                10,
                15,
                20,
                25
            ]
            : [
                1,
                2,
                3,
                4,
                5
            ]
    return {
        page: 1,
        page_length: page_lengths[0],
        um: page_lengths.slice(), // <-- duplicate the array
        total: 0,
    }
}); // <--- do not invoke here

import.js

import {
    pagination,
} from "export.js"

var foo = pagination(); // invoke at this point

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

JavaScript: Invoking a function within another function via an HTML document

I have a unique scenario where I have one function inside another and I am looking to call the inner function from an HTML document. <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="style.css"> ...

Determine the exact width of text without rounding in Javascript

I'm facing an issue with a div element that I'm manipulating using Javascript to change its position. The problem is that it's rounding off incorrectly. #hi{ background-color: blue; width: 2px; height: 10px; position: absolu ...

Invoking two asynchronous functions in React, where the first function relies on the state modified by the second function

Currently, I am working on an app that utilizes the Geoapify API. Within this app, I have implemented three primary API functions. Users are presented with two options on the user interface: they can manually search for cities or utilize their current loca ...

Loop through the elements of one array using the indexes from a second array

Hey there! I need help with the following code snippet: let names = ["josh", "tony", "daniel"]; let arrayplaces = ["30", "60", "90"]; names.forEach((elem, indexed) => { const num2 = arrayp ...

Ajax request causing bootstrap success message to have shorter visibility

I encountered an issue with my ajax form that retrieves data using the PHP post method. Instead of utilizing the alert function in JavaScript, I decided to use a bootstrap success message. However, there is a problem as the message only appears for less th ...

Deactivate Bootstrap Button Pills and Activate Change with an Alternate Button

Seeking a solution for my multi-step form utilizing Bootstrap nav-pills. Currently, users can navigate through the steps without completing the required form fields first. I am looking to disable this feature and trigger content changes with a different bu ...

Storing formatted user input in an array with VueJS: A step-by-step guide

Looking for assistance that relates to the following question Vue.js: Input formatting using computed property is not applying when typing quick I am facing a challenge in extracting formatted values from text inputs and storing them in an array. I intend ...

Retrieve every potential option of a dropdown menu

One of my tasks involves working with a select field that has various values: africa europe america asia oceania Whenever a value is selected, I aim to apply a specific CSS class to a particular div, with the name of the CSS class matching the selected v ...

Tips for utilizing rest parameters in JavaScript/Typescript: Passing them as individual parameters to subsequent functions, rather than as an array

Question about Typescript/JavaScript. I made my own custom log() function that allows me to toggle logging on and off. Currently, I am using a simple console.log(). Here is the code: log(message: any): void { console.log(message) } Recently, I decid ...

The welcome message in Discord.js is displaying the user as a string of numbers instead of their real username

bot.on('guildMemberAdd', user =>{ const greetingMessage = new Discord.MessageEmbed() .setTitle(`Welcome to the server, ${user.user}! We're glad you've joined.`) const channel = user.guild.channels.cache.find(channel =&g ...

Cannot find a function within the Promise

Here is the code snippet I am working with: var c = function(address, abiJson){ var _ = this; this.data = { wallet: false, account:{ address: false }, contract:{ addre ...

How can one determine the most accurate box-shadow values?

I am trying to extract the precise box-shadow parameters from a CSS style rule generated by the server. My main focus is determining whether the element actually displays a visible shadow or not. There are instances where the shadow rule is set as somethi ...

Instead of returning a single array of data from a form as expected, Jquery is returning two arrays

Having a bit of trouble with my code involving radio buttons and arrays using Jquery. I'm trying to record the selected values into one array, but it's creating separate arrays for each fieldset. Here's what I have: <script> $(doc ...

Demonstration of using .queue() and .dequeue() in relation to $.queue() and $.dequeue()

I successfully completed an animation using animate(), queue(), and dequeue(). However, I recently discovered that jQuery also offers jquery.queue() or $.queue() and jquery.dequeue() or $.dequeue(). Can anyone assist me in understanding these new terms w ...

Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7. I attempted using db.collection('items').where.. but unfortunately, this method is not supported. For instance: (collection) ...

Vue-CLI encounters a hiccup while attempting to fetch an image from the assets directory

Currently working with vue-cli 3 and I've implemented this style: .login-page { ... background: url("~@/assets/login-bg.jpg") no-repeat; background-size: cover; background-position: center; ... } However, upon running yarn serve, an error ...

Error in initializing UI-router wrapper due to circular dependency in Angular.js

I've been following the angular style guide of John Papa (https://github.com/johnpapa/angular-styleguide#routing) and implementing a custom wrapper for angular ui-router as suggested. However, I'm encountering an issue with the wrapper causing a ...

My node.js code is not producing the expected result. Can anyone point out where I may be going wrong?

I've been working on a BMI calculator where I input two numbers, they get calculated on my server, and then the answer is displayed. However, I'm having trouble receiving the output. When I click submit, instead of getting the answer, I see a lis ...

Resolve the flexible width problem when inserting text in pptxgenjs

I am attempting to replicate a layout similar to this https://i.sstatic.net/HDUhV.png However, the text width is taking up more space than anticipated. https://i.sstatic.net/CnKIB.png The current text behavior resembles that of a div, but I would like ...

The selector bar in Cypress does not work properly when trying to find the text 'foo' using jQuery

Having trouble with jQuery selectors in my Cypress tests. I often need to use jQuery selectors to locate elements based on text or unique generated numbers instead of using cy.contains(). Any help would be greatly appreciated! https://i.sstatic.net/FjqzJ ...