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); // []
}
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"]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With