[英]How to determine what javascript object has been returned
In instances when a javascript function returns an object, what is a good way of determining what exactly it is that you got?在 javascript 函数返回一个对象的情况下,有什么好方法可以确定您得到的究竟是什么?
If I do this:如果我这样做:
alert(myFunction(this));
And I get back simply [object Object]
, what are some useful things that I can do to determine what it is?我简单地返回
[object Object]
,我可以做哪些有用的事情来确定它是什么?
if debugging don't use alert, use the console instead如果调试不使用警报,请改用控制台
console.log(myFunction(this));
console.dir(myFunction(this));
console.error(myFunction(this));
//etc
If you are trying to determine the type of object and do something depending on what it is use typeof or instanceof如果您正在尝试确定对象的类型并根据它的内容执行某些操作,请使用typeof或instanceof
Using typeof使用 typeof
var something = myFunction(this);
if(typeof something === "string"){
console.log("It's a string");
}
Using instanceof使用 instanceof
var something = myFunction(this);
if(something instanceof HTMLElement){
console.log("It's an html element");
}
Use console.log
method to display data in your console instead of alert :使用
console.log
方法在控制台中显示数据而不是 alert :
console.log(myFunction(this));
In some browsers you can use console.dir
, so you can get more details about the object :在某些浏览器中,您可以使用
console.dir
,以便您可以获得有关该对象的更多详细信息:
console.dir(myFunction(this));
Example例子
var myObj = {foo: 'bar'} alert(myObj); console.log(myObj); //Check your console, you can see the object console.dir(myObj); //You can see the object with more details
Hope this helps.希望这可以帮助。
Example : http://jsfiddle.net/6daL71zd/示例: http : //jsfiddle.net/6daL71zd/
You can use:您可以使用:
1) 1)
console.log(myFunction(this))
to print out to the console打印到控制台
(this can be accessed via your browser's developer tools...the 'F12' key on your keyboard should open it) (这可以通过浏览器的开发人员工具访问...键盘上的“F12”键应该可以打开它)
2) 2)
var output = document.createTextNode(JSON.stringify(myFunction(this)));
document.body.appendChild(output);
to print out on the page.在页面上打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.