我发现自己经常被 console.log()
绊倒,因为它是异步的。
我的意思是它倾向于不捕获特定时间点的变量值。
它对于简单值来说效果很好,因为对具有立即值的变量进行赋值会将变量绑定(bind)到一个全新的对象。例如
var x = 3;
console.log(x); //prints 3
x=4;
但是当您开始使用通过引用绑定(bind)的对象时,事情就会变得……违反直觉。例如
var obj = {x: 3};
console.log(x); //prints 4!
obj.x = 4;
是否有其他一些我可以使用的日志记录函数可以在我的代码中为我提供调用时对象的状态?我正在寻找同步的或者至少看起来是来自它产生的结果的东西。
如果它能跨平台工作,我会很高兴,但我会很高兴能得到一个能在 Chrome 中工作的产品。
最佳答案
根据要求,摘要:
对于简单的可序列化对象,console.log(JSON.stringify(x))
效果很好:
var x = { foo: 17, bar: "quux" };
console.log(JSON.stringify(x))
// => {"foo":17,"bar":"quux"}
对于 HTML 元素,console.log(x.outerHTML)
效果很好。
var x = document.getElementsByTagName('p');
console.log(Array.prototype.map.call(x, function(e) {
return e.outerHTML;
}));
// => ["<p>foo</p>", "<p id="bar">bar</p>"]
<p>foo</p>
<p id="bar">bar</p>
如果对象不可序列化,您可能需要深入研究它们并提取可序列化部分:
var x = { content: { foo: 17, bar: "quux" } };
x.self = x;
console.log(JSON.stringify(x.content));
// => {"foo":17,"bar":"quux"}
如果这些技巧都不适用,您可能需要深度克隆该对象( this answer 提供了一个非常好的 clone
函数,我在下面使用它,还有许多注意事项):
function clone(item) {
if (!item) { return item; } // null, undefined values check
var types = [ Number, String, Boolean ],
result;
// normalizing primitives if someone did new String('aaa'), or new Number('444');
types.forEach(function(type) {
if (item instanceof type) {
result = type( item );
}
});
if (typeof result == "undefined") {
if (Object.prototype.toString.call( item ) === "[object Array]") {
result = [];
item.forEach(function(child, index, array) {
result[index] = clone( child );
});
} else if (typeof item == "object") {
// testing that this is DOM
if (item.nodeType && typeof item.cloneNode == "function") {
var result = item.cloneNode( true );
} else if (!item.prototype) { // check that this is a literal
if (item instanceof Date) {
result = new Date(item);
} else {
// it is an object literal
result = {};
for (var i in item) {
result[i] = clone( item[i] );
}
}
} else {
// depending what you would like here,
// just keep the reference, or create new object
if (false && item.constructor) {
// would not advice to do that, reason? Read below
result = new item.constructor();
} else {
result = item;
}
}
} else {
result = item;
}
}
return result;
}
var el = document.querySelector('p');
x = { el: el, content: { foo: 17, bar: "quux" } };
console.log("el.attributes[0]: changed", x);
console.log("el.attributes: empty", clone(x));
el.setAttribute('changed', 'true');
<p>foo</p>
在最坏的情况下,您可以随时暂停执行:
var el = document.querySelector('p');
for (var i = 0; i < 1000; i++) {
el.textContent = "Iteration #" + i;
// check 458th iteration:
if (i == 458) {
console.log("Accurate:", el);
debugger;
console.log("Not accurate:", el);
}
}
<p></p>
关于javascript - console.log 的非 volatile 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169234/