admin管理员组文章数量:1208155
The intended output of my function is {"name": "bob", "number": 1}
, but it returns [object Object]
. How can I achieve the desired output?
function myfunc() {
return {"name": "bob", "number": 1};
}
myfunc();
The intended output of my function is {"name": "bob", "number": 1}
, but it returns [object Object]
. How can I achieve the desired output?
function myfunc() {
return {"name": "bob", "number": 1};
}
myfunc();
Share
Improve this question
asked Jan 3, 2016 at 5:54
kilojouleskilojoules
10.1k21 gold badges82 silver badges157 bronze badges
2
|
2 Answers
Reset to default 11Haha this seems to be a simple misunderstanding. You are returning the object, but the toString()
method for an object is [object Object]
and it's being implicitly called by the freecodecamp console.
Object.prototype.toString()
var o = {}; // o is an Object
o.toString(); // returns [object Object]
You can easily verify you actually are returning an object using your own code:
function myfunc() {
return {"name": "bob", "number": 1};
}
var myobj = myfunc();
console.log(myobj.name, myobj.number); // logs "bob 1"
If you try console.log(ob.name)
it should display bob
{}
in JS is a shorthand for an object. You can convert your object to string using the toString()
method.
本文标签: javascript function to return object returns object ObjectStack Overflow
版权声明:本文标题:javascript function to return object returns [object Object] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738738861a2109729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
alert()
ing the result? – Blender Commented Jan 3, 2016 at 5:57