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

259
Visualizações
how to declare multiple variables with the same value

in order to prevent creating every single variable with same type I declared them this way:

  data () {
    return {
      today,tomorrow: new Date(),
    };
  },

all I get in my intellij terminal is an error

error 'today' is not defined no-undef

have I mistaken the syntax or the process is not possible ?

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

0

You are returning an object, in an object by using the comma , you are listing different properties of the object, since you don't define today before typing the comma, you tell the object that today is now undefined.

It is better to just declare both properties or not use an object.

function data() {
  let sameVariable = new Date();
  return {
    today: sameVariable,
    tomorrow: sameVariable,
  };
}

This way you can now acces the variables with obj.today and obj.tomorrow

Maybe take a look in how objects work (thats when returning something with {...}) You are returning an Object in your example

about 4 years ago · Juan Pablo Isaza Relatório

0

Simple. you can't. In JS object and JSON syntaxt you can't declare and init many varialbes in one expression.

if you need many vars with the same value, just return it from setup function.

setup(){
   const now = new Date();
   return {
      today: now,
      tomorow: now
   }
}

you can create it in "create" function.

created(){
   this.today = new Date();
   this.tomorrow = new Date();
}

But be aware. Date is instance of Data class. So if two variables points to it, then you have to remember that changing props of today will affect tomorrow. Eg. if your today and tommorow variables points to one Date object then today.setHours will affect tommorow as well.

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