jQuery Deferred& console.log - 奇怪的行为

时间:2012-08-13 10:34:58

标签: jquery console jquery-deferred

考虑以下示例:

var $_dfd = $.Deferred(),
    $_x = {};

$_x = {
    a: 1,
    b: 2
};

console.log($_x); // Gives {a: 1, b: 2, more: {c: 3, d: 4}} <== Weirdness here
console.log($_x.a); // Gives 1
console.log($_x.more); // Gives undefined

$_dfd.pipe(function($_x) {
    $_x.more = {
        c: 3,
        d: 4                    
    };

    return $_x;
});

$_dfd.resolve($_x).done(function($_x) {
    console.log($_x); // Gives {a: 1, b: 2, more: {c: 3, d: 4}}
});

我对控制台.log输出#1感到非常困惑。有两个问题需要回答:

  1. 第一个console.log输出中变量$_x的真实状态是什么?

  2. 如果在使用延迟时,console.log不是一种理解变量状态的安全方法,那么还有哪些更好的选择呢?

  3. 谢谢!

1 个答案:

答案 0 :(得分:0)

必须使用JSON.stringify(),详见以下帖子:

Bug in console.log?

相关问题