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

181
Views
Skipping passing default parameters to a function

Let's say I have the following function:

function enumObject(obj, inherited=false, methods=false) {
    console.log('*'.repeat(20));
    for (let prop in obj) {
        if (!inherited && !obj.hasOwnProperty(prop)) continue;
        if (!methods && typeof obj[prop] === 'function')  continue;
        console.log(prop, '-->', obj[prop]);
    }
}

And I want to call it as: enumObject(o, methods=true). Is there a short-hand way to basically ignore any parameters except aside from the ones I explicitly set? I know in the above example it's a bit trivial, as I can just do: enumObject(o, false, true), but let's say I had a function that had 20 arguments, and I only wanted to pass one of them a non-default value. How would that be done? the one thing that comes to mind is to collect all properties into an object, for example something like:

function enumObject(obj, props={}) {
    // props: methods, inherited
    for (let prop in obj) {
        if (!props.inherited && !obj.hasOwnProperty(prop)) continue;
        if (!props.methods && typeof obj[prop] === 'function')  continue;
        console.log(prop, '-->', obj[prop]);
    }
}
let o = {x: 1, y: 2, z() {}}
enumObject(o)
enumObject(o, props={methods: true})

about 4 years ago · Juan Pablo Isaza
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!