Probably a naive question - if this snippet is executed (I'm running with Node.js v12.16.3, same result in Chrome apart from the error type):
const obj = {}
Object.defineProperty(obj, 'a', {
writable: false,
value: 13
})
obj.a = 14
It will, obviously, throw an error, but the message string of that error has strange representation of an object:
TypeError: Cannot assign to read only property 'a' of object '#<Object>'
The question is - why it's stringified as #<Object>?
Defining a toString method on the object will render it as if it were called with Object.prototype.toString.call(obj):
const obj = {
toString () {
return 'stringified obj'
}
}
Object.defineProperty(obj, 'a', {
writable: false,
value: 13
})
console.log(`${obj}`)
obj.a = 14
TypeError: Cannot assign to read only property 'a' of object '[object Object]'
While console.log outputs correctly. How to make that TypeError output the correct string representation? What does it use internally?
"use strict";. Youre using babel in your snippet. Babel uses strict-mode. Without strict mode no error will be shown. Maybe it has something to do with the strict mode and the error - handling of it.