Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Error object properties [duplicate]

I am trying to learn JS and am confused about object properties, in the example below I have used an undefined function to generate an error and I am trying to get properties and methods of the error object. While e.message does print the error message I am not able to get message as error object property. What is happening here?

try{  
  unknownFunction();// undefined function here 
}catch(e){  
  console.log(e); // it is ReferenceError  
  console.log(e.message);//message  
  console.log(typeof e); // object  
  console.log(e instanceof ReferenceError); // true  
  console.log(e === ReferenceError); // false  
  for(var propertyName in e) {  
   console.log("Name "+propertyName+" and Value "+e[propertyName]);  
  } // []
  let allKeys = Object.keys(e);
  console.log(allKeys);// []
  let fnKeys  = allKeys.filter(key => typeof myObj[key] == 'function');
  console.log(fnKeys); // []
}
like image 325
Swaraj Yadav Avatar asked Jan 16 '17 04:01

Swaraj Yadav


1 Answers

message is simply not an enumerable property:

try{  
  unknownFunction();// undefined function here
}catch(e){ 
  console.log( Object.getOwnPropertyDescriptor( e, 'message' ) )
  /* {
      value: "unknownFunction is not defined", 
      writable: true,
      enumerable: false,
      configurable: true
  } */

  let allKeys = Object.getOwnPropertyNames(e);
  console.log(allKeys); 
  // ["stack","message"]

}
like image 185
Paul Avatar answered Sep 20 '22 15:09

Paul