Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

javascript - Consecutive calls to console.log produce inconsistent results

Ok, I'm completely dumbfounded by this. (and I might be overlooking something obvious but...)

I have 2 consecutive calls to console.log. There isn't anything else between them

console.log($state);
console.log($state.current);

and here's an image of the produced results

Console.log screenshot

Why do the 2 produce different "current" objects? How can this happen?


Context:

Those calls are made inside an ajax call while resolving a route dependencies. If you need more code or context let me know.

Confirmed the same issue in Chrome and Firefox

Ajax call and wrapper function (no modifications whatsoever)

normaCtrl.publicNorma = ['$http', '$state', '$stateParams', 'baseUrl', function ($http, $state, $stateParams, baseUrl)
{
    var id = $stateParams.id;
    return $http.get(baseUrl + "api/public/norma/" + id).then(
        function (response) {
            console.log($state);
            console.log($state.current);
            console.log($state.current.title);
            return response.data;
        }
    );
}];

Possible related questions

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, here's the answer for those that stumble upon this.

Short Answer

Console.log shows deep mutable objects at the last state of execution, not at the state when console.log was called.

More details

Basically, when working with mutable deep objects, Console.log stores the reference to said object instead of storing an object clone.

Since there is a time gap between storing and visualization, when you click the arrow for further inspection what you're seeing is actually the current state of the object and not the the state of the object when console.log was called.

One way to always make sure you're using an "object snapshot" is to call Json.stringify or use console.dir when available.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...