11

I'm doing some JavaScript debugging with Chrome dev tools and found the following oddity.

How is it possible that date has a different value when the object is expanded?

1
  • Can you show how the object is created? When I create an object with a date it shows the same value. Commented Jul 15, 2015 at 8:39

2 Answers 2

13

You should be careful with chrome console when printing objects. Please note that chrome does evaluations when printing / expanding objects as well as it does it asynchronously (meaning other code may execute in the meantime and change the object). Always try to print to String for debugging, rather than printing the object itself.

I made a very simple example to illustrate the problem.

<div id="foo"></div> console.log($('#foo')); //expected output [div#foo...] $('#foo').attr('id','hello'); 

The actual output in the chrome console is:

example image

Try it yourself here (JSFiddle).

Sign up to request clarification or add additional context in comments.

Comments

0

This happens when you define a non enumerable property on an object like this

Object.defineProperty(res, 'url', { value: '/api/url' }); 

To fix this you have to make it enumerable like this

Object.defineProperty(res, 'url', { value: '/api/url', enumerable: true }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.