Console logging object shows 2 different values

Deadly 提交于 2021-02-11 10:13:15

问题


This is insane, or is it?! If I access departuredate directly I get the correct value of 2016-05-18.

console.log(searchFactory.search.searchparams.journeys[0].departuredate); // 2016-05-18 
console.log(searchFactory.search.searchparams.journeys[0] //stored in the object as 2010-05-10 

However if I console.log() the entire object as in the attached screenshot, it is reset to todays date. How is that even possible? Ultimately the value is assigned to my Factory, which ends up being 2016-05-10 - today. Somewhere along the line the date is getting changed to todays date and for the life of me I don't understand how.

$scope.$watch('date', function(newDate) {
    searchFactory.search.searchparams.journeys[0].departuredate = moment(newDate.startDate).format("YYYY-MM-DD");
    searchFactory.search.searchparams.journeys[0].returndate = moment(newDate.endDate).format("YYYY-MM-DD");
    console.log(searchFactory.search.searchparams.journeys[0]);    
});

I am going insane, trying to figure this out.

Essentially I am watching the date scope item, which all workds fine. The newDate then gets assigned to my factory for storage and later use. As you can see I am simply logging the object to see the value. You can see in the screenshot how it carries two different values... which to my believe was impossible until now.

Any pointers would be appreciated.


回答1:


This is(was) a known bug in webkit that still has yet to hit chrome 4 years later.

As of today, dumping an object (array) into console will result in objects' properties being read upon console object expansion (i.e. lazily). This means that dumping the same object while mutating it will be hard to debug using the console.

This change starts generating abbreviated previews for objects / arrays at the moment of their logging and passes this information along into the front-end. This only happens when the front-end is already opened, it only works for console.log(), not live console interaction.

Seen here.

You can use obj.toString() in the meantime to log as you intend.



来源:https://stackoverflow.com/questions/37130320/console-logging-object-shows-2-different-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!