bodine
bodine

Reputation: 1823

Is there a way to get console.table to execute property accessor functions?

I have a collection of objects with many getter properties. I'd like to be able to print a subset of these values in the console using console.table. The current implementations do not seem to allow this, is there a workaround?

let obj = {
    get prop() {
        return "getter";
    }
    id:1
}

console.table([obj]); // prints only index, and id
console.table([obj], ["prop"]); // prints only index, but still not "prop"

Upvotes: 2

Views: 615

Answers (1)

Dekel
Dekel

Reputation: 62636

The console.table api is still non-standard.

Currently - chrome is not capable of working with getters values, and if you will check firefox - all values that are defined by getters are printed as undefined.

Note that this has nothing to do with hasOwnProperty or 'prop' in obj (both works great), however it might be related to Object.getOwnPropertyDescriptor.

In your example (for both Chrome & Firefox):

Object.getOwnPropertyDescriptor(obj, 'id').value
// 1
Object.getOwnPropertyDescriptor(obj, 'prop').value
// undefined

However if this is the case I would expect from Chrome to also display the prop column (even with the undefined values in it).

You can use this snippet to test on your browser:

let obj = {
    get prop() {
        return "getter";
    },
    id:1
}

console.log('Log "t in obj"')
for (var t in obj) {
  console.log(t, obj[t]);
}
console.log('')

console.table([obj]);
console.table([obj], ["id"]);
console.table([obj], ["prop"]); // currently returns a column of 'undefined' in firefox, and in chrome that column doesn't exists.

Upvotes: 1

Related Questions