Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

322
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda