Javascript 如何查看带有 alert() 的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5753931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I view an object with an alert()
提问by Melova1985
I tried to do a debug but I am having problems. Now I try with alert()
. For example I want to see the value of:
我尝试进行调试,但遇到了问题。现在我尝试使用alert()
. 例如,我想查看以下值:
var product = { ProductName: $('!Answer_Response[0]').val(),
UnitPrice: $('#Price').val(),
Stock: $('#Stock').val()
};
When I say alert(product)
it just gives me [object Object]
. How can I make alert show what's really there?
当我说它alert(product)
只是给了我[object Object]
。我怎样才能让警报显示真正有什么?
回答by Pranay Rana
you can use the JSON.stringify()
method found in modern browsers and provided by json2.js.
您可以使用JSON.stringify()
在现代浏览器中找到并由json2.js提供的方法。
var myObj = {"myProp":"Hello"};
alert (JSON.stringify(myObj)); // alerts {"myProp":"Hello"};
or
或者
also check this library : http://devpro.it/JSON/files/JSON-js.html
还要检查这个库:http: //devpro.it/JSON/files/JSON-js.html
回答by aya
you can use toSource method like this
你可以像这样使用 toSource 方法
alert(product.toSource());
回答by Aron Rotteveel
If you want to easily view the contents of objects while debugging, install a tool like Firebugand use console.log
:
如果您想在调试时轻松查看对象的内容,请安装Firebug 之类的工具并使用console.log
:
console.log(product);
If you want to view the properties of the object itself, don't alert
the object, but its properties:
如果要查看对象本身的属性,不要查看alert
对象,而是查看其属性:
alert(product.ProductName);
alert(product.UnitPrice);
// etc... (or combine them)
As said, if you really want to boost your JavaScript debugging, use Firefox with the Firebug addon.You will wonder how you ever debugged your code before.
如前所述,如果你真的想提升你的 JavaScript 调试,使用带有 Firebug 插件的 Firefox。您会想知道您以前是如何调试代码的。
回答by KooiInc
This is what I use:
这是我使用的:
var result = [];
for (var l in someObject){
if (someObject.hasOwnProperty(l){
result.push(l+': '+someObject[l]);
}
}
alert(result.join('\n'));
If you want to show nested objects too, you could use something recursive:
如果你也想显示嵌套对象,你可以使用递归的东西:
function alertObject(obj){
var result = [];
function traverse(obj){
for (var l in obj){
if (obj.hasOwnProperty(l)){
if (obj[l] instanceof Object){
result.push(l+'=>[object]');
traverse(obj[l]);
} else {
result.push(l+': '+obj[l]);
}
}
}
}
traverse(obj);
return result;
}
回答by Znarkus
You should really use Firebug or Webkit's console for debugging. Then you can just do console.debug(product);
and examine the object.
您真的应该使用 Firebug 或 Webkit 的控制台进行调试。然后你可以做console.debug(product);
和检查对象。
回答by Sagar Maha
Try this:
尝试这个:
alert(JSON.parse(product) );
回答by Ruhul Amin
Use Javascript native JSON.stringify
method. To visualise it in a nicer way, you can use, like: JSON.stringify(obj,null, 4)
使用 Javascript 本机JSON.stringify
方法。要以更好的方式可视化它,您可以使用,例如:JSON.stringify(obj,null, 4)
var obj = {data:[{"empmenuid":"1","empid":null,"deptid":"66","aliasid":"66","firstname":"66","lastname":"66","sin":"66","status":"66","empclass":"66","hiredate":"66","seneoritydate":"66","separationdate":"66"},{"empmenuid":"3","empid":null,"deptid":"12","aliasid":"12","firstname":"12","lastname":"12","sin":"12","status":"12","empclass":"12","hiredate":"12","seneoritydate":"12","separationdate":"12","recalldate":"12","martialstatus":"12","gender":"12","pager":"12","locid":"12","jobtitle":"12","jobtitlestart":"12","fullpart":"12","manager":"12","managername":"12","middlename":"12","nickname":"12","paytype":"12","payfreq":"12"}],
recordType : 'object'};
alert(JSON.stringify(obj,null, 4));
回答by Flash
Depending on which property you are interested in:
根据您感兴趣的房产:
alert(product.ProductName);
alert(product.UnitPrice);
alert(product.Stock);
回答by dice
alert (product.UnitName + " " + product.UnitPrice + " " + product.Stock)
or else create a toString() method on your object and call
或者在您的对象上创建一个 toString() 方法并调用
alert(product.toString())
But I have to agree with other posters - if it is debugging you're going for then firebug or F12 on IE9 or chrome and using console.log is the way to go
但我必须同意其他海报 - 如果它正在调试,那么你要在 IE9 或 chrome 上使用 firebug 或 F12 并使用 console.log 是要走的路
回答by Blazes
alert( JSON.stringify(product) );