8

Trying to understand why the two cross-browser properties of Javascript Error object, namely "name" and "message", can't be found using the "for ... in" method

// error code
...
}catch( err ){

  // in FF this lists 3 properties for fileName, lineNumber and columnNumber...
  // but NOT name or message!
  for(var propertyName in err) {
    $( '#diags' ).append( 'err property: ' + propertyName + ', 
       value: ' + err[ propertyName ] + '<br>' );
  }
  // this line prints fine:
  $( '#diags' ).append( 'Error - name:' + err.name + ', message: ' + err.message + '<br>' );
}

Edit

I am asked what is name and message. These are properties (are they though?) which all Errors have in any browser... so in the above code I have added an extra line of code which shows that these "attributes" or whatever they are print fine

Edit2

Following Mati's helpful answer I did a bit of searching. This seems to answer the "inspection" question: Is it possible to get the non-enumerable inherited property names of an object?

Community
  • 1
  • 1
mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • What is `name` or `message`? – Amberlamps Mar 27 '13 at 09:25
  • Javascript Error object has always been a bit of a mess in some browsers. It should have a `.message` property. Anything else is proprietary. [This article](https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Error) shows how the Error object can be standardized - see section titled "Custom Error Types". – Beetroot-Beetroot Mar 27 '13 at 09:39
  • @Beetroot-Beetroot Yes I had found that page... hence my surprise that these two "properties" (if that is what they are) are not actually listed using the for ... in loop above... I remain puzzled – mike rodent Mar 27 '13 at 17:25

1 Answers1

8

A for...in loop does not iterate over non–enumerable properties.

var e = new Error('a');

console.log(e.propertyIsEnumerable('name'));
console.log(e.propertyIsEnumerable('message'));
console.log(e.propertyIsEnumerable('fileName'));
console.log(e.propertyIsEnumerable('lineNumber'));
console.log(e.propertyIsEnumerable('columnNumber'));

for(var prop in e)
{
    console.log(prop + ': ' + e[prop]);
}

Output

false
false
true
true
true
fileName: index.html
lineNumber: 25
columnNumber: 0
Mati
  • 1,378
  • 15
  • 20
  • 4
    Thanks! Now the question you've all been waiting for: how do I find what non-enumerable properties there are? I know, I know: they're non-enumerable, so you can't enumerate them! Is this the answer? In languages with which I'm more familiar there are usually options to "inspect" an object ... anything like that in JS? – mike rodent Mar 27 '13 at 19:56
  • 1
    @mike: see [the answers at this question](http://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object). (I suppose you've already figured this out -- but for those who end up here nowadays, like me, it may be still useful) – robert4 Jul 17 '15 at 20:16
  • But this begs the question - "Why on earth is the Error.message property non-enumerable??" If I look at the Javascript specification at Mozilla it doesn't say so. This seems screwy!! – BobDoolittle Jun 03 '16 at 00:23
  • @BobDoolittle Actually it says so in the beginning '/---/ for...in statement iterates over the enumerable properties of an object /---/'. Why? See relevant discussion here: http://stackoverflow.com/questions/14984533/what-are-the-benefits-of-making-properties-non-enumerable – Mati Jun 06 '16 at 13:50
  • @Mati - I know what it does. But that still doesn't explain why you would want to NOT iterate over the message property of an Error object. That seems like something you'd WANT to be iterable/enumerable (i.e. in a for loop). What was the reasoning behind making that field non-enumerable for for loops etc? – BobDoolittle Jun 07 '16 at 14:46
  • 1
    @BobDoolittle I agree, message and and name ARE st you might want to show up in enum ops. I cant think of anything better than Its just seems to be the common characteristic for built in objects. I've been looking into the subject and have come to understanding that enumerability in JS is a rather idiosyncratic property. – Mati Jun 08 '16 at 07:43