Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.create alters console output of proto object in Chrome?

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.

enter image description here

UPDATE Bug filed: https://code.google.com/p/chromium/issues/detail?id=478522

like image 653
pbo Avatar asked Apr 19 '15 11:04

pbo


1 Answers

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: enter image description here

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.

like image 105
Benjamin Gruenbaum Avatar answered Sep 20 '22 04:09

Benjamin Gruenbaum