Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda