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

309
Views
How to return a default parameter while calling object?

I am using routes variable to return routes to various urls.

export const routes = {
    root: {  path: '/' },
    login: { path: '/login' },
    users: { path: '/users'}
}

Now, I have to use it like routes.login.path now. But is there any way that if nothing is passed after the parameter, it takes 'path' by default. Something like this: routes.login should automatically return '/login'

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You want routes.login to return the string "/login", and you want to access .path through that string. I would recommend you to rethink the the way you are approaching this problem.

Nevertheless, there are two ways to achieve this:

One is to assign a path property onto each string like so:

export const routes = {
    root: Object.assign('/', {  get path() {return String(this)} }),
    login: Object.assign('/login', {  get path() {return String(this)} }),,
    users: Object.assign('/users', {  get path() {return String(this)} }),
}

The other is to override the toString method of the object:

export const routes = {
    root: {  path: '/', toString() {return this.path} },
    login: { path: '/login', toString() {return this.path} },
    users: { path: '/users', toString() {return this.path} }
}

Of course, you can clean this up with a separate class:

// First Method
class Route extends String {
  get path() { return String(this) }
}

export const routes = {
    root: new Route('/'),
    login: new Route('/login'),
    users: new Route('/users')
}

// Second Method
class Route {
  constructor(data) {
    Object.assign(this, data);
  }
  toString() {
    return this.path;
  }
}

export const routes = {
    root: new Route({  path: '/' }),
    login: new Route({ path: '/login' }),
    users: new Route({ path: '/users' })
}
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!