7

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

pbo
  • 537
  • 4
  • 13
  • chrome is right you are wrong – webduvet Apr 19 '15 at 11:21
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/create – webduvet Apr 19 '15 at 11:49
  • @webduvet do you understand my question? Object.create is alerting what is being logged to the console for b. I'm not talking about c and the new object created. – pbo Apr 19 '15 at 11:53
  • @pbo well maybe that's the case, perhaps if you were more explicit what you actually expect and what chrome gives you it might help :) – webduvet Apr 19 '15 at 11:55
  • Are you reading my question? I'm asking why is Baz not being outputted. It's right there in my description. – pbo Apr 19 '15 at 11:57
  • You are expecting the constructor, but you are logging its instance. – Leo Apr 19 '15 at 12:04
  • well on my chrome is all as usual and expected. b is show as `Baz {baz: true}` – webduvet Apr 19 '15 at 12:04
  • Oh interesting on my Mac that's not the case. @Leo I'm expecting the instance but chrome names the instance with the constructor. – pbo Apr 19 '15 at 12:06
  • well maybe you meant to `console.log(c)` not `b` ? – webduvet Apr 19 '15 at 12:10
  • @pbo I added a screenshot to your question, is that what you are talking about? If it's not please revert my edit. – Leo Apr 19 '15 at 12:10
  • @Leo Excellent! Thank you so much. That definitely helps show the issue more clearly :) – pbo Apr 19 '15 at 12:11
  • it does not change on my box. Version 41.0.2272.118 (64-bit) – webduvet Apr 19 '15 at 12:20
  • @webduvet Looks like what I have above was introduced in v 42. If you update you will get the behavior I show above. Looks like Chrome IS wrong :) – pbo Apr 19 '15 at 12:22
  • I just have noticed the below updated answer. well spotted. :) – webduvet Apr 19 '15 at 12:24

1 Answers1

5

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.

Paul Irish
  • 47,354
  • 22
  • 98
  • 132
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • What's odd is I'm not talking about the new object. The var b in my example was created solely via a constructor function. I don't see why creating another object alters what chrome outputs. – pbo Apr 19 '15 at 12:03
  • @pbo ` console.log(b)` logs `Baz {baz: true}`, and `console.log(c)` logs `Object {baz: true}` (although it _is_ a Baz, because it was created with Object.create and not a constructor. – Benjamin Gruenbaum Apr 19 '15 at 12:04
  • @BenjaminGruenbaum No, both log `Object {baz: true}`. – Siguza Apr 19 '15 at 12:07
  • @Siguza then it's a bug, this is what Chrome does on my box: http://i.imgur.com/I9wbgry.png – Benjamin Gruenbaum Apr 19 '15 at 12:08
  • @BenjaminGruenbaum Well here's mine: http://i.imgur.com/UxvadIy.png btw, Chrome 42.0.2311.90 (64-bit), Mac – Siguza Apr 19 '15 at 12:12
  • I'm on Chrome 41 on Mac, lemme update to 42 and check real quick. – Benjamin Gruenbaum Apr 19 '15 at 12:13
  • @Siguza sweet find, I can definitely reproduce this as a regression from Chrome 41 to Chrome 42 (On 64bit mac) I'll ping a friend who works on the dev tools. – Benjamin Gruenbaum Apr 19 '15 at 12:15
  • Thanks @BenjaminGruenbaum I knew it was a bug :) – pbo Apr 19 '15 at 12:19
  • @pbo I owe you an update, this is [the related issue](https://code.google.com/p/chromium/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Week%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=&id=339099) I've spoken to a very involved Googler about it and I've asked a friend to take a stab at a fix, if he doesn't I'll take a stab myself. Good find. – Benjamin Gruenbaum Apr 19 '15 at 19:58