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

133
Visualizações
Acessing Multiple Functions Inside Single Function In an Object

So I Have The Code as Follows:

const Solver = new Object
Solver.circle = function(r){
area = Math.PI * r**2
perimeter = Math.PI * 2*r 
return "Area:" + area + " Perimeter:" + perimeter
}

Now I Would Like To Get the value of area and perimeter seperately like using Something like this

Solver.circle(2).area  \\ 12.57
Solver.circle(2).perimeter \\ 12.57
Solver.circle(2)  \\ Area:12.57 Perimeter:12.57

Is there any way i could achieve something like this. If Not What could be the best Alternative Other than having different Main Functions like Solve.circlearea(2) or Solve.circleperi(2)

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

0

Sure! Just return an object!

const Solver = new Object
  Solver.circle = function(r){
  area = Math.PI * r**2
  perimeter = Math.PI * 2*r 
  return {area, perimeter}
}

Note that you can also do return {area: area, perimeter: perimeter}, but it is not necessary and JS will understand you even if you don't do that. So here you are just returning an object and in your usage you are calling the function and accessing the property from the returned object.

If you replace your function with this, your API example will work as is!

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