Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

392
Visualizações
Why am I getting the Uncaught TypeError: Assignment to constant variable?

I am trying to create a vending machine app. I have created a class constructor in one file and used export so that I can import that into my other js file.

When I try and create a class object for the DrinkItem I get a Uncaught TypeError: Assignment to constant variable

please see the code below:

JS file 1 code

export class DrinkItem{
    constructor(id, name, flavour,cost,stockAvailable){

        this.id = id,
        this.name = name,
        this.flavour = flavour,
        this.cost = cost,
        this.stock = stockAvailable
    }          
}

JS file 2 code

import { DrinkItem } from "../classes/drinkItem.js"

/****************************
 * Generates an ID for task *
 ****************************/

 const createItemId = () => `${Math.floor(Math.random() * 1000)} - ${new Date().getTime()}`.value

//array for purchased drink item
let menu = [];

/*************************************************
 * Create at least 5 different DrinkItem objects *
 *************************************************/


DrinkItem = new DrinkItem(
    createItemId,
    "Fanta",
    "grape",
    18,
    10
);

let test = DrinkItem.push(menu)
console.log(test)

The error happens at the new DrinkItem() bit, so maybe I am doing this incorrectly, I have looked at MDN, but it does not really explain it well, or maybe I miss understand it.

What am I doing wrong? I am using the class name DrinkItem for the const as I want it to link to the constructor.

What I want to do is create/instantiate at least 5 different DrinkItem objects inside of the menu array.

once the 5 DrinkItem objects are created I can push that into the menu array, but for now I am looking to see how to create the at least 1 object from the class.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just the last piece of code: You are assigning a value to a class

let drinkItem = new DrinkItem(
    createItemId,
    "Fanta",
    "grape",
    18,
    10
);

Then here another error, menu is array and DrinkItem is object, push the last in the first one

let test = menu.push(drinkItem)
console.log(test)
about 4 years ago · Juan Pablo Isaza Relatório

0

Change to this:

let drinkItem = new DrinkItem( // DrinkItem variable name is already used for the class, you cannot give your instance the same variable name
    createItemId(), // you want to call the function, not assign the function to DrinkItem.id
    "Fanta",
    "grape",
    18,
    10
);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda