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})