admin管理员组文章数量:1310513
I have created an object person
and created 2 properties for that person
object in JavaScript. I passed data to that object as:
personA = new person("Michel","Newyork");
personB = new person("Roy","Miami");
What I need is, how to display both personA
& personB
values at same time through JavaScript?
I have created an object person
and created 2 properties for that person
object in JavaScript. I passed data to that object as:
personA = new person("Michel","Newyork");
personB = new person("Roy","Miami");
What I need is, how to display both personA
& personB
values at same time through JavaScript?
- 3 Where do you want to display the values? And what do you mean by "at the same time"? – Dek Dekku Commented May 17, 2013 at 13:43
-
What does the
person
constructor take? What property does it assign too? – SomeShinyObject Commented May 17, 2013 at 13:44 -
Show us the code for
person
! – Bergi Commented May 17, 2013 at 13:58 - For display object value Please refer this link technologiessolution.blogspot.in/2013/11/… – Nikunj K. Commented Nov 28, 2013 at 6:28
3 Answers
Reset to default 2If you simply want to display them on the console,for debug purposes, use
console.log (personA, personB);
If you want to alert them to the screen :
alert (JSON.stringify (personA), JSON.stringify (personB));
If you want to change a DOM element to contain the values :
domElement.innerHTML = personA.name + ' from ' + personA.loc + ' and ' +
personB.name + ' from ' + personB.loc;
assuming here than name
and loc
are the property names you used.
Of course you can use any of these methods in any context depending on your requirements.
I'm going to assume person
takes a name
and loc
(for location) since you didn't clarify:
var personArr = []; //create a person array
//Then push our people
personArr.push(personA);
personArr.push(personB);
for (var i = 0; i < personArr.length; i++) {
console.log(personArr[i].name);
console.log(personArr[i].loc);
}
If you're talking about literally at the same time, I won't say it's impossible, just not possible with JavaScript unless you use WebWorkers and those don't fair too well in IE.
You could also consider this approach:
var persons = {
person: [],
add: function(name, city)
{
this.person.push({
'name': name,
'city': city
});
}
};
persons.add("Michael", "New York");
persons.add("Roy", "Miami");
Output:
for(x in persons.person)
{
console.log(x + ":");
for(y in persons.person[x])
{
console.log(y + ": " + persons.person[x][y]);
}
}
------
0:
name: Michael
city: New York
1:
name: Roy
city: Miami
本文标签: javascriptDisplay object valuesStack Overflow
版权声明:本文标题:javascript - Display object values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741820718a2399346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论