Why doesn't console.log() take a snapshot of the passed variables?

后端 未结 3 1317
长发绾君心
长发绾君心 2020-12-31 01:59

I\'ve ran into some really weird behavior with javascript today. I think I got it somehow figured out now, but I\'d like to know if what I think is going on is really happen

相关标签:
3条回答
  • 2020-12-31 02:33

    I've also seen this behavior and it sure looks like a reference is posted. To get around this I used the clone() method in jQuery on the things I wanted to log.

    0 讨论(0)
  • 2020-12-31 02:42

    Yes, this is what's going on. I would guess it's done to minimize the overhead of console.log() calls. Things could get out of control if every object was deep cloned for every log call.

    When I need to workaround this, I either JSON.stringify() or shallow clone the object before passing it to console.log().

    0 讨论(0)
  • 2020-12-31 02:52

    There is console.dir() for what you want in Firebug.

    In general, it is not possible to print every level of nested properties, since objects can contain circular references like var a = {}; var b = {a: a}; a.b = b;

    Implementing a perfect clone method is very hard - I guess it would have to basically just dump the whole memory, and logging would take awfully long. Think about console.log(window)...

    0 讨论(0)
提交回复
热议问题