Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!