How can I make console.log show the current state of an object?

前端 未结 11 1773
走了就别回头了
走了就别回头了 2020-11-22 00:28

In Safari with no add-ons (and actually most other browsers), console.log will show the object at the last state of execution, not at the state when conso

相关标签:
11条回答
  • 2020-11-22 01:07

    Just print whole object on console.

    console.dir(object);
    
    0 讨论(0)
  • 2020-11-22 01:08

    I defined an utility:

    function MyLog(text) {
        console.log(JSON.stringify(text));
    }
    

    and when I want to log on console I simply do:

    MyLog("hello console!");
    

    It works very well!

    0 讨论(0)
  • 2020-11-22 01:09

    using Xeon06's hint, you may parse his JSON in an object, and here is the log function I now use to dump my objects :

    function odump(o){
       console.log($.parseJSON(JSON.stringify(o)));
    }
    
    0 讨论(0)
  • 2020-11-22 01:10

    I may be shot for suggesting this, but this can be taken one step further. We can directly extend the console object itself to make it more clear.

    console.logObject = function(o) {
      (JSON.stringify(o));
    }
    

    I don't know if this will cause some type of library collision/nuclear meltdown/rip in the spacetime continuum. But it works beautifully in my qUnit tests. :)

    0 讨论(0)
  • 2020-11-22 01:19

    Vanilla JS:

    @evan's answer seems best here. Just (ab)use JSON.parse/stringify to effectively make a copy of the object.

    console.log(JSON.parse(JSON.stringify(test)));
    

    JQuery specific solution:

    You can create a snapshot of an object at a certain point in time with jQuery.extend

    console.log($.extend({}, test));
    

    What is actually happening here is jQuery is creating a new object with the test object's content, and logging that (so it will not change).

    AngularJS (1) specific solution:

    Angular provides a copy function that can be used to the same effect: angular.copy

    console.log(angular.copy(test));
    

    Vanilla JS wrapper function:

    Here is an function which wraps console.log but will make a copy of any objects before logging them out.

    I wrote this in response to a few similar but less robust functions in the answers. It supports multiple arguments, and will not try to copy things if they are not regular objects.

    function consoleLogWithObjectCopy () {
      var args = [].slice.call(arguments);
      var argsWithObjectCopies = args.map(copyIfRegularObject)
      return console.log.apply(console, argsWithObjectCopies)
    }
    
    function copyIfRegularObject (o) {
      const isRegularObject = typeof o === 'object' && !(o instanceof RegExp)
      return isRegularObject ? copyObject(o) : o
    }
    
    function copyObject (o) {
      return JSON.parse(JSON.stringify(o))
    }
    

    example usage: consoleLogWithObjectCopy('obj', {foo: 'bar'}, 1, /abc/, {a: 1})

    0 讨论(0)
  • 2020-11-22 01:21

    I think you're looking for console.dir().

    console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.

    The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:

    console.log(JSON.parse(JSON.stringify(obj)));

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