Where are the properties of the name and message of the Error object indicated?

Trying to understand why the two cross-browser properties of the Javascript Error object, namely the "name" and "message", cannot 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 the name and message are. These are properties (are they though?), Which all errors have in any browser ... therefore, in the above code, I added an additional line of code that shows that these "attributes" or whatever they are printed.

Edit2

After Mati's helpful answer, I searched a bit. This seems to answer the “validation” question: Is it possible to get non-enumerable inherited property names of an object?

+4
source share
1 answer

A for ... in a loop does not iterate over 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 
+6
source

All Articles