zarte
V2EX  ›  问与答

求问个 js 的问题,之前貌似问过。

  •  
  •   zarte · Dec 11, 2018 · 1211 views
    This topic created in 2750 days ago, the information mentioned may be changed or developed.
    ...未定义 user.name
    console.log(user)
    user.name = 'sssss'
    ...
    

    为什么编译后会输出 name,不是应该没有 name 吗。以上代码是在 vue 里的

    3 replies    2018-12-11 10:40:05 +08:00
    a4854857
        1
    a4854857  
       Dec 11, 2018
    因为对象是引用类型的。所以 console.log 打印出来的是显示该对象在内存中最新的状态。
    如果你是 console.log(JSON.stringtify(uer));
    这样的话,打印出来是就是 console.log 那一瞬间 user 的内容。
    我经常这样调试对象。
    愚见,有错误的话望指出
    SakuraKuma
        2
    SakuraKuma  
       Dec 11, 2018
    和 Vue 无关。

    console.log ,你不点开还好,点开就会去拿现在状态的了。

    要真实的请用断点。

    所以避免 log debug 吧。

    ```javascript
    o = {};console.log(o);o.a=1
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5861 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 75ms · UTC 02:13 · PVG 10:13 · LAX 19:13 · JFK 22:13
    ♥ Do have faith in what you're doing.