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

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

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 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!