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

141
Visualizações
Using one object value in another value on the same object Javascript

My question is somewhat simple, but IDK if I haven't made the search with the right keywords, but my problem is:

When defining an object in JS/TS, how can I use one value from the obj into another key, something like:

const person = {
  firstName: 'Régis',
  middleName: 'Faria',
  fullName: `${firstName + middleName}` // Here is where IDK how to do it. I've tried with 'this.' but no success
}

Hope someone can help me out. Thanks in advance!!

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

0

Construct the object with firstName and middleName, then add the fullName:

const person = {
  firstName: 'Régis',
  middleName: 'Faria',
}
person.fullName = `${person.firstName}  ${person.middleName}`;
about 4 years ago · Juan Pablo Isaza Relatório

0

To use this keyword, you must have an instance of an object, to do so, you can create a Person class and initialize a person.

class Person {
  constructor({ firstName, middleName }) {
    this.firstName = firstName
    this.middleName = middleName
  }
  get fullName() {
    return `${this.firstName} ${this.middleName}`
  }
}

const person = new Person({
  firstName: 'Régis',
  middleName: 'Faria',
})

console.log(person.fullName)

Otherwise, you must set firstName and middleName first:

const firstName = 'Régis'
const middleName = 'Faria'
const person = {
  firstName,
  middleName,
  fullName: `${firstName} ${middleName}`
}

console.log(person.fullName);

I personally prefer to use TypeScript:

// create a Person type
type Person {
  firstName: string
  lastName: string
  fullName: string
}

// then it's ok to have variable
const firstName = 'Régis'
const middleName = 'Faria'

// then your person
const person: Person = {
  firstName,
  middleName,
  fullName: `${firstName} ${middleName}`
}
console.log(person.fullName)
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