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

326
Visualizações
What is the difference between writing this code with and without class?

so I've been getting confused about this particular issue, hopefully someone more experienced can help me out on this.

Why does this work:

This will add the value as a key, and the value of the key to true. So if you say something like this

const mySet = new Set()
mySet.add("hello")

it will return this: {hello: true}.

But if I do something like this outside ES6 classes, so more like this:

const car = {
  color: "red"
}

car[built] = 2019

This will say built is undefined, is this only usable in ES6 classes?

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

0

car[built] = 2019 built is an undefined variable

Change it to car["built"] = 2019 or car.built = 2019

var car = {};
var built = "some key";
car[built] = true;

then car is {"some key": true}

about 4 years ago · Juan Pablo Isaza Relatório

0

It works in the class syntax as you have defined a method that accepts a variable that is used as a key, and thus successfully creates a key in the object.

In the car[built] syntax it considers built here as a variable and thus fails in this case.

Whenever defining a key using obj[key] syntax, the key should either be a value or a string.

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