Error properties are not enumerable in Javascript
If you transverse a regular object:
var obj = {foo: 'bar', baz: true};
for (var i in obj) {
console.log(i, obj[i]);
}
The following will be output:
foo bar
baz true
But, what if you transverse an instance of Error
:
var e = new Error('Something went wrong');
for (var i in e) {
console.log(i, e[i]);
}
That won't print anything. In addition, Object.keys(e)
will return []
.
Let's add some custom properties.
var e = new Error('Something went wrong');
e.foo = 'bar';
for (var i in e) {
console.log(i, e[i]);
}
Then those will be displayed:
foo bar
Confused? Then check Error
default properties are not enumerable.
Written by Alberto Pose
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Enumerable
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#