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

202
Visualizações
Objeto JavaScript con función usando "esto"

Acabo de empezar a aprender JavaScript y me gustaría hacerlo funcionar. Escribí una función que verifica tu edad según la fecha actual y la información de tu cumpleaños. La función funciona bien (sí, sé que probablemente haya una forma más rápida y clara de crear esta función).

 var today = new Date(); var currentDay = today.getDate(); var currentMoth = today.getMonth() + 1 var currentYear = today.getFullYear() function birthdayCheck(birth_day, birth_month, birth_year) { if (currentMoth > birth_day || (birth_day <= currentDay && currentMoth >= birth_month)) { return (currentYear - birth_year) } else { return (currentYear - birth_year) - 1 } }

Quiero usar esta función dentro de Object .

 let birthday = { birthYear: 2010, birthDay: 3, birthMonth: 8, myAge: birthdayCheck(this.birthDay, this.birthMonth, this.birthYear), } console.log(birthday.myAge);

si escribo el número en lugar de this.birthDay, this.birthMonth, this.birthYear funciona bien, pero no quiero volver a escribir los números, quiero reutilizar lo que ya está dentro del bloque, ¿cómo puedo hacer que funcione? ?

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

0

 let birthday = { birthYear: 2010, birthDay: 3, birthMonth: 8, birthdayCheck: function() { var today = new Date(); var currentDay = today.getDate(); var currentMoth = today.getMonth() + 1 var currentYear = today.getFullYear() if (currentMoth > this.birthDay || (this.birthDay <= currentDay && currentMoth >= this.birthMonth)) { this.myAge = currentYear - this.birthYear; } else { this.myAge = (currentYear - this.birthYear) - 1; } } }; birthday.birthdayCheck(); console.log(birthday.myAge);

 var today = new Date(); var currentDay = today.getDate(); var currentMoth = today.getMonth() + 1 var currentYear = today.getFullYear() function birthdayCheck(birth_day, birth_month, birth_year) { if (currentMoth > birth_day || (birth_day <= currentDay && currentMoth >= birth_month)) { return (currentYear - birth_year) } else { return (currentYear - birth_year) - 1 } } let birthday = { birthYear: 2010, birthDay: 3, birthMonth: 8, } birthday.myAge = birthdayCheck(birthday.birthDay, birthday.birthMonth, birthday.birthYear); console.log(birthday.myAge); birthday.myAge = birthdayCheck.apply(null, Object.values(birthday).reverse()); console.log(birthday.myAge);

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