This question already has an answer here:
这个问题已经有了答案:
Typically if we just use alert(object);
it will show as [object Object]
. How to print all the content parameters of an object in Javascript?
通常,如果我们使用alert(对象);它将显示为[对象对象]。如何在Javascript中打印对象的所有内容参数?
295
If you are using Firefox, alert(object.toSource())
should suffice for simple debugging purposes.
如果您使用的是Firefox, alert(object.toSource())应该满足简单的调试目的。
592
This will give you very nice output with indented JSON object:
这将为您提供具有缩进JSON对象的非常好的输出:
alert(JSON.stringify(YOUR_OBJECT_HERE, null, 4));
The second argument alters the contents of the string before returning it. The third argument specifies how many spaces to use as white space for readability.
第二个参数在返回字符串之前修改字符串的内容。第三个参数指定了有多少空格作为可读性的空白。
72
Aside from using a debugger, you can also access all elements of an object using a foreach
loop. The following printObject
function should alert()
your object showing all properties and respective values.
除了使用调试器之外,还可以使用foreach循环访问对象的所有元素。下面的printObject函数应该警告()对象显示所有属性和各自的值。
function printObject(o) {
var out = '';
for (var p in o) {
out += p + ': ' + o[p] + '\n';
}
alert(out);
}
// now test it:
var myObject = {'something': 1, 'other thing': 2};
printObject(myObject);
Using a DOM inspection tool is preferable because it allows you to dig under the properties that are objects themselves. Firefox has FireBug but all other major browsers (IE, Chrome, Safari) also have debugging tools built-in that you should check.
使用DOM检查工具更可取,因为它允许您挖掘对象本身的属性。Firefox有FireBug,但是所有其他主流浏览器(比如Chrome, Safari)也有内置的调试工具,你应该检查一下。
24
If you just want to have a string representation of an object, you could use the JSON.stringify
function, using a JSON library.
如果你只想要一个对象的字符串表示,你可以使用JSON。stringify函数,使用JSON库。
12
Print content of object you can use
打印可以使用的对象的内容。
console.log(obj_str);
you can see the result in console like below.
您可以在如下的控制台看到结果。
Object {description: "test"}
For open console press F12 in chrome browser, you will found console tab in debug mode.
在chrome浏览器中打开控制台按F12,你会在调试模式下找到控制台选项卡。
8
You should consider using FireBug for Javascript debugging. It will let you interactively inspect all of your variables, and even step through functions.
您应该考虑使用FireBug进行Javascript调试。它会让你交互式地检查所有的变量,甚至是通过函数。
8
You could Node's util.inspect(object) to print out object's structure.
您可以节点的util.inspect(对象)打印对象的结构。
It is especially helpful when your object has circular dependencies e.g.
当对象具有循环依赖关系时,它特别有用。
$ node
var obj = {
"name" : "John",
"surname" : "Doe"
}
obj.self_ref = obj;
util = require("util");
var obj_str = util.inspect(obj);
console.log(obj_str);
// prints { name: 'John', surname: 'Doe', self_ref: [Circular] }
It that case JSON.stringify throws exception: TypeError: Converting circular structure to JSON
这这种情况下JSON。stringify抛出异常:类型错误:将循环结构转换为JSON。
7
Use dir(object). Or you can always download Firebug for Firefox (really helpful).
使用dir(对象)。或者你也可以为Firefox下载Firebug(真的很有用)。
3
Javascript for all!
Javascript。
String.prototype.repeat = function(num) {
if (num <0) {
return '';
} else {
return new Array(num + 1).join(this);
}
};
function is_defined(x) {
return typeof x !== 'undefined';
}
function is_object(x) {
return Object.prototype.toString.call(x) === "[object Object]";
}
function is_array(x) {
return Object.prototype.toString.call(x) === "[object Array]";
}
/**
* Main.
*/
function xlog(v, label) {
var tab = 0;
var rt = function() {
return ' '.repeat(tab);
};
// Log Fn
var lg = function(x) {
// Limit
if (tab > 10) return '[...]';
var r = '';
if (!is_defined(x)) {
r = '[VAR: UNDEFINED]';
} else if (x === '') {
r = '[VAR: EMPTY STRING]';
} else if (is_array(x)) {
r = '[\n';
tab++;
for (var k in x) {
r += rt() + k + ' : ' + lg(x[k]) + ',\n';
}
tab--;
r += rt() + ']';
} else if (is_object(x)) {
r = '{\n';
tab++;
for (var k in x) {
r += rt() + k + ' : ' + lg(x[k]) + ',\n';
}
tab--;
r += rt() + '}';
} else {
r = x;
}
return r;
};
// Space
document.write('\n\n');
// Log
document.write('<' + (is_defined(label) ? (label + ' ') : '') + Object.prototype.toString.call(v) + ' >\n' + lg(v));
};
// Demo //
var o = {
'aaa' : 123,
'bbb' : 'zzzz',
'o' : {
'obj1' : 'val1',
'obj2' : 'val2',
'obj3' : [1, 3, 5, 6],
'obj4' : {
'a' : 'aaaa',
'b' : null
}
},
'a' : [ 'asd', 123, false, true ],
'func' : function() {
alert('test');
},
'fff' : false,
't' : true,
'nnn' : null
};
xlog(o, 'Object'); // With label
xlog(o); // Without label
xlog(['asd', 'bbb', 123, true], 'ARRAY Title!');
var no_definido;
xlog(no_definido, 'Undefined!');
xlog(true);
xlog('', 'Empty String');
2
You can give your objects their own toString methods in their prototypes.
您可以在它们的原型中为对象提供它们自己的toString方法。
2
You can use json.js from http://www.json.org/js.html to change json data to string data.
您可以使用json。从http://www.json.org/js.html将json数据更改为字符串数据。
2
You can also use Prototype's Object.inspect() method, which "Returns the debug-oriented string representation of the object".
您还可以使用Prototype的object .inspect()方法,该方法“返回对象的面向调试的字符串表示”。
http://api.prototypejs.org/language/Object/inspect/
http://api.prototypejs.org/language/Object/inspect/
1
Simple function to alert contents of an object or an array .
Call this function with an array or string or an object it alerts the contents.
用于警告对象或数组内容的简单函数。用一个数组或字符串或一个对象来调用这个函数,它会通知内容。
Function
函数
function print_r(printthis, returnoutput) {
var output = '';
if($.isArray(printthis) || typeof(printthis) == 'object') {
for(var i in printthis) {
output += i + ' : ' + print_r(printthis[i], true) + '\n';
}
}else {
output += printthis;
}
if(returnoutput && returnoutput == true) {
return output;
}else {
alert(output);
}
}
Usage
使用
var data = [1, 2, 3, 4];
print_r(data);
0
Internet Explorer 8 has developer tools which is similar to Firebug for Firefox. Opera has Opera DragonFly, and Google Chrome also has something called Developer Tools (Shift+Ctrl+J).
Internet Explorer 8有与Firefox类似的开发工具。Opera有Opera蜻蜓,谷歌Chrome也有一个叫做Developer Tools (Shift+Ctrl+J)的东西。
Here is more a more detailed answer to debug Javascript in IE6-8: Using the IE8 'Developer Tools' to debug earlier IE versions
下面是在IE6-8中调试Javascript的更详细的答案:使用IE8的“开发工具”来调试早期的IE版本。
-3
I faced similar problem, The reason for it was i make use of ajax to fetch data. In this case i had made two asynchronous ajax call. In one i just return string msg and show in alert. In second ajax call i fetch arraylist in json format and decode it in js. So my second request use to process first and i was getting alert of object.
我遇到了类似的问题,原因是我使用ajax来获取数据。在本例中,我进行了两个异步ajax调用。在其中一个,我只返回字符串msg,并显示在警报中。在第二个ajax调用中,我以json格式获取arraylist并将其解码为js。所以我的第二个请求是先处理,然后我得到了对象的警告。
So just check. 1. alert should contain string. 2. If u get arrayList or any other Object decode it.
所以只要检查。1。通知应包含字符串。2。如果你得到arrayList或任何其他对象解码它。
All the best!
所有最好的!