The value of Vue version 2 within an object nested within a class property was updated without being modified

Using vue v2, I am in the process of developing a Single Page Application (SPA). Within the script section of my vue component, I have defined an Offer class as follows:

class Offer {
       constructor(Tariff, TariffCopy, billing, at, fa, backs) {
       this.Tariff = Tariff;
       this.BaseTariff = TariffCopy;
    }

    changeSAC(p1, p2) {
       ...
       this.Tariff.provAufwand.SO = this.BaseTariff.provAufwand.SO + Number(p1);
    }
}

In this context, Tariff and TariffCopy are instances created from a custom class named Tariff, also specified within the script section of the same component. Essentially, they hold the same values; however, Tariff is subject to modifications, while BaseTariff's values remain static.

class Tariff{
    constructor(provAufwand) {
        this.provAufwand = provAufwand;
    }
}

where

provAufwand= {
    SO: 1,
    HO5: 2,
    HO10: 3,
    HO20: 4
}

Upon invoking Offer.changeSAC(p1,p2) - where p1 and p2 are linked to number inputs via v-model="p1" and v-model="p2", it appears that the value of this.BaseTariff.provAufwand.SO is unexpectedly modified despite no explicit changes being made to it in the code. What could be causing this connection between them in my code?

Answer №1

If you initialized your Tariff instances in this manner:

const provAufwand= {
    SO: 1,
    HO5: 2,
    HO10: 3,
    HO20: 4
};
const tariff = new Tariff(provAufwand);
const tariffCopy = new Tariff(provAufwand);

It explains why altering BaseTariff or tariffCopy also impacts tariff, as they are all pointing to the same provAufwand object.

When passing provAufwand to the Tariff constructor, it passes a reference of the object instead of the actual value.

Providing an object or an array to a function results in it being passed by reference.

This implies that modifications made within the function affect the corresponding object/array outside the function.

To address this, utilize the spread syntax when transferring provAufwand while creating a new Tariff instance.

const provAufwand= {
    SO: 1,
    HO5: 2,
    HO10: 3,
    HO20: 4
};
const tarrif = new Tariff({...provAufwand});
const tarrifCopy = new Tariff({...provAufwand});

By doing so, distinct objects are passed when instantiating your class objects.

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

Leveraging Array.map within Angular Interpolation

Is it possible to achieve the following in HTML using Angular 2+? {{ object.array.map((o) => o.property ) }} Whenever I try to execute this code, it crashes the Angular application. Do I need to utilize Pipes or any other technique? ...

How can you continuously calculate and show the total quantity of items in a list?

Currently, I have lists set up in my sidebar and I'm looking to include a label displaying the number of items within each list. To provide a better understanding of what I'm aiming for, I've put together a JSFiddle demonstration at the fol ...

Insert a CSS Class into an HTML div element with JQuery

I'm faced with a bit of a challenge. Here's the scenario: <div class="portItem"></div> <div class="portItem"></div> <div class="portItem"></div> <div class="p ...

Is it possible for me to avoid html tags inside a class without using the xmp tag?

There are a few ways to approach this question. It's important to decide which method will be most beneficial for your specific needs... Is it possible for JavaScript to recreate the deprecated <xmp> tag using an "xmp" class? Can we replicate ...

"Troubleshooting: Issue with React's useState and useEffect functions in conjunction with localStorage

Encountering a challenge with a React component using useState and useEffect hooks to handle state and maintain it in localStorage. The component aims to create a collapsible section with a heading and content, where the expanded state is stored in localSt ...

Can you explain the meaning of the symbol '&$checked'?

import React from 'react'; import Checkbox from '@material-ui/core/Checkbox'; import { createMuiTheme, makeStyles, ThemeProvider } from '@material-ui/core/styles'; import { orange } from '@material-ui/core/colors'; ...

The RadImageEditor onSaving Event does not permit the sending of response scripts

I am using a custom RadImageEditor control with a version of 2013.1.220.40. The control is placed on a page and displayed in a RadWindow. My goal is to either pass a URL or not pass it (for Update and Add scenarios, respectively). After cropping the image ...

Leveraging NodeJS to handle server-side tasks and operations

Background: I am exploring the use of NodeJS for a project that involves scraping and storing content in Mongo. This process needs to be automated according to a set schedule. In addition, I need functions that can extract items from the Mongo database, o ...

Changing a value in Vue without a preexisting key will not refresh the display

In each row of my table, I have a select box. I want the checkbox to represent a value in the data that doesn't currently exist. <tr v-for="item in someData"> <input type="checkbox" v-model="item.selected"></td> <input ty ...

What is the process for including a scope in an Angular.js HTTP controller?

I am looking to access a $scope variable within this controller in order to utilize Angular functions like ng-click and ng-change in my HTML. Currently, I am only able to use the $http function but unable to execute any Angular methods on it. I am struggli ...

Prevent a child DIV from adopting the style of its parent page

Imagine a scenario where you have a webpage with its own unique stylesheet. You decide to include a popup or modal at the end of the page using Javascript, but you want this popup to have its own distinct style. Since the content within the modal is dynami ...

Invoke a parent method from a nested child component in Vue

After setting up a project with vue-cli using the webpack template, I decided to incorporate a reusable bootstrap modal dialog in the App component. To achieve this, I created a method called showMessage in the App component that handles displaying the mod ...

How can one update transitive dependencies to a specific newer version in npm to address CVE vulnerabilities?

Here are the packages that require upgrading. I attempted to update the version and signature of the packages listed in package-lock.json. However, whenever I run npm i after modifying package-lock.json, the changes made to package-lock.json disappear. ...

Attempting to replace an AngularJS object with another service results in it being reinitialized upon using ctrl + f5 in the application

In my AngularJS application, I have two services. One service is used to declare objects with default values, while the other service is responsible for assigning new values to those objects. Everything works fine in the controllers after rewriting, but wh ...

Exploring the differences between arrays and filter functions within MongoDB aggregation operations

Recently, I crafted a sophisticated pipeline for my database: let orders = await Order.aggregate( { $unwind: "$candidate", }, { $lookup: { from: "groups", localField: & ...

Is there a way to link the scrolling of two ag-grid data tables together for synchronized movement?

I have integrated ag-grid into my Angular project and I am looking to display two data tables (ag-grid) stacked on top of each other. Both data tables should scroll together, meaning when I scroll in table 1 or table 2, the content of both tables should m ...

Assigning properties to the constructor using `this.prop`

Within a React class component, I have multiple methods and the component receives various props. I am contemplating whether I should assign each prop as this.propName in the constructor and then access them using this.propName. For your reference, below ...

Incorporate object keys into an array using JavaScript

Query: I'm working on a JavaScript project and I have an array that looks like this: [6.7, 8, 7, 8.6]. I need to transform this array into an array of objects with named properties: [{y: 6.7} , {y: 8}, {y: 7}, {y: 8.6}]. Can someone guide me on how to ...

Download or access blob using FileSaver.js on iOS Chrome and Safari

I'm utilizing the docxtemplater library in JavaScript to generate a docx document. It works perfectly on desktop browsers, but I'm encountering issues on iOS. When attempting to download or open the document on iOS, a new tab opens in the brows ...

Tips for preloading an ENTIRE webpage

I am looking for a way to preload an entire web page using JavaScript in order to have it cached in the user's browser. While I know how to preload images with JS, my goal is to also preload the entire page itself. For example, on my website, there ...