JavaScript:console.log()给出的结果与alert()不同 [英] JavaScript: console.log() gives different results than alert()
问题描述
console.log()是否应该在JavaScript中调用变量时打印出变量的值?这是我的假设,但当我在Firefox(使用Firebug)或谷歌Chrome(并使用内置的开发工具)中运行下面的代码时,我似乎得到了数组的最终值,而不是当时的数组。如果我使用alert()语句,它们会打印出我期望的内容 - 调用alert()语句时数组的值。
Is console.log() supposed to print out the value of a variable at the time it's called in your JavaScript? That was my assumption but when I run the code below in either Firefox (using Firebug) or Google Chrome (and use the built-in dev tools), I seem to get the "final" value of an array rather than the value of the array at that time. If I use alert() statements they print out what I would expect - the value of an array at the time the alert() statement is called.
var params = new Array();
var tmp = new Array('apple', 'banana', 'cat');
for (var i=0; i < tmp.length; i++) {
params[tmp[i]] = [];
}
console.log(params);
/*
SHOWS IN CONSOLE:
- []
+ apple ["jan", "feb", "mar", "apr"]
+ banana ["jan", "feb", "mar", "apr"]
+ apple ["jan", "feb", "mar", "apr"]
*/
alert( print_arr(params) );
/*
ALERT BOX TEXT:
[apple]:
[banana]:
[cat]:
*/
console.log('===========================================');
var tmp2 = new Array('jan', 'feb', 'mar', 'apr');
for (var i=0; i < tmp.length; i++) {
for (var j=0; j < tmp2.length; j++) {
params[tmp[i]].push(tmp2[j]);
}
}
console.log(params);
/*
SHOWS IN CONSOLE:
- []
+ apple ["jan", "feb", "mar", "apr"]
+ banana ["jan", "feb", "mar", "apr"]
+ apple ["jan", "feb", "mar", "apr"]
*/
alert( print_arr(params) );
/*
ALERT BOX TEXT:
[apple]:jan,feb,mar,apr
[banana]:jan,feb,mar,apr
[cat]:jan,feb,mar,apr
*/
function print_arr(arr) {
var str = '';
for (var k in arr) {
str += '[' + k + ']:' + arr[k].toString() + "\n";
}
return str;
}
推荐答案
正如我在评论中所说 console.log(obj)
不记录字符串表示,它记录对内存中实际javascript对象的引用。因此,对对象所做的任何更改都将反映在记录的实例中。
As I said in the comments console.log(obj)
does not log a string representation, it logs a reference to the actual javascript object in the memory. So any changes made to the object will get reflected in the logged instance.
如果要跟踪所做的渐进式更改,请将对象转换为字符串并打印到喜欢 console.log(JSON.stringify(params))
。
If you want to trace the progressive changes made, then convert the object to a string and print to like console.log(JSON.stringify(params))
.
此外你还没有使用 params
作为数组,您将其用作地图。所以将 params
更改为对象 var params = {}
Also you are not using params
as an array, you are using it as an map. so change params
to an object var params = {}
将 params
更改为对象并使用 JSON.stringify
进行记录
Change params
to an object and use JSON.stringify
to log it
var params = {};
var tmp = new Array('apple', 'banana', 'cat');
for (var i=0; i < tmp.length; i++) {
params[tmp[i]] = [];
}
console.log(JSON.stringify(params));
/*
SHOWS IN CONSOLE:
- []
+ apple ["jan", "feb", "mar", "apr"]
+ banana ["jan", "feb", "mar", "apr"]
+ apple ["jan", "feb", "mar", "apr"]
*/
alert( print_arr(params) );
/*
ALERT BOX TEXT:
[apple]:
[banana]:
[cat]:
*/
console.log('===========================================');
var tmp2 = new Array('jan', 'feb', 'mar', 'apr');
for (var i=0; i < tmp.length; i++) {
for (var j=0; j < tmp2.length; j++) {
params[tmp[i]].push(tmp2[j]);
}
}
console.log(JSON.stringify(params));
/*
SHOWS IN CONSOLE:
- []
+ apple ["jan", "feb", "mar", "apr"]
+ banana ["jan", "feb", "mar", "apr"]
+ apple ["jan", "feb", "mar", "apr"]
*/
alert( print_arr(params) );
/*
ALERT BOX TEXT:
[apple]:jan,feb,mar,apr
[banana]:jan,feb,mar,apr
[cat]:jan,feb,mar,apr
*/
function print_arr(arr) {
var str = '';
for (var k in arr) {
str += '[' + k + ']:' + arr[k].toString() + "\n";
}
return str;
}
演示:小提琴
这篇关于JavaScript:console.log()给出的结果与alert()不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!