I was playing around today when I noticed that some of my objects in Chrome's console were being displayed as Object
instead of the constructor function name.
This was odd, so I boiled it down to the following code:
function Baz() {
this.baz = true;
}
var b = new Baz();
var c = Object.create(b);
console.log(b); // why is b outputting with Object not Baz?
In the above code b
, is not created via a Object.create
and yet when logged it says Object. I don't have a typo there, and mistakenly asking about c. The log of b has been altered when I haven't even touched that object. Creating another instance c
, should not alter b
.
This has to be a Chrome bug right? Is there anyway to get Chrome to correctly report Baz
here?
This is important for debugging purposes.
UPDATE Bug filed: https://code.google.com/p/chromium/issues/detail?id=478522
Update: This is indeed a regression between Chrome 41 and Chrome 42. It's being tracked here: http://crbug.com/478522
Chrome 41's output:
Chrome 42's output:
They've made improvements to syntax highlighting as you type in the dev tools and this probably broke. I've pinged a friend who is deeply involved with the dev tools. Nice find.
No. The problem you describe is very real.
Objects created with constructors will have their name displayed when logging them and generally better debugging experience in Chrome (and in node/io.js).
For this reason - I avoid Object.create
for prototypical inheritance in my own code although I prefer it conceptually.
I think you understand this - but I still want to clarify for future readers. Note that the inheritance still happens with the Object.create version - the only difference is in how the object is logged and treated in the debugger.
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