admin管理员组文章数量:1355107
function Luminary(radius, orbitRadius, speed, children) {
this.radius = radius;
this.orbitRadius = orbitRadius;
this.speed = speed;
this.children = children;
}
function initSolarSystem() {
var moon = new Luminary(0.02, 0.2, 0.0015, []);
var earth = new Luminary(0.1, 0.7, 0.001, [moon]);
var sun = new Luminary(0.3, 0.0, 0.0, [earth]);
return sun;
}
var solarSystem = initSolarSystem();
I have the code above in JS. How can I access for example the radius of earth using the solarSystem object? The following returns Undefined
alert(solarSystem.children.radius);
How should I call children in a recursive function as follows:
function draw(obj) { // draw Current Object if (obj.children != undefined) { draw(obj.children); } } draw(solarSystem);
Can Someone please help me?
function Luminary(radius, orbitRadius, speed, children) {
this.radius = radius;
this.orbitRadius = orbitRadius;
this.speed = speed;
this.children = children;
}
function initSolarSystem() {
var moon = new Luminary(0.02, 0.2, 0.0015, []);
var earth = new Luminary(0.1, 0.7, 0.001, [moon]);
var sun = new Luminary(0.3, 0.0, 0.0, [earth]);
return sun;
}
var solarSystem = initSolarSystem();
I have the code above in JS. How can I access for example the radius of earth using the solarSystem object? The following returns Undefined
alert(solarSystem.children.radius);
How should I call children in a recursive function as follows:
function draw(obj) { // draw Current Object if (obj.children != undefined) { draw(obj.children); } } draw(solarSystem);
Can Someone please help me?
Share Improve this question edited Nov 21, 2017 at 12:36 TessavWalstijn 1,7362 gold badges22 silver badges37 bronze badges asked Nov 21, 2017 at 12:22 dieKoderindieKoderin 1,5823 gold badges19 silver badges43 bronze badges 1-
4
solarySystem.children[0].radius
– martskins Commented Nov 21, 2017 at 12:26
2 Answers
Reset to default 3I have the code above in JS. How can I access for example the radius of earth using the solarSystem object? The following returns Undefined alert(solarSystem.children.radius);
solarSystem.children
is an array, so use solarSystem.children[0].radius
How should I call children in a recursive function as follows.
function draw(obj)
{
// draw Current Object
if (obj.children != undefined)
{
obj.children.forEach( s => draw(s) ); //invoke draw in a loop
//draw(obj.children[0]); //use
}
}
draw(solarSystem);
First of all your .children
is an array. so call .children[i].radius
.
Second:
if (obj.children != undefined) { draw(obj.children); }
You call here once the draw function for the full children
array. So you need to implement a for loop.
For this there are a lot options this is my approach:
function Luminary(name, radius, orbitRadius, speed, children = []) {
this.name = name;
this.radius = radius;
this.orbitRadius = orbitRadius;
this.speed = speed;
this.children = children;
}
function initSolarSystem() {
var moon = new Luminary("moon", 0.02, 0.2, 0.0015);
var earth = new Luminary("earth", 0.1, 0.7, 0.001, [moon]);
var sun = new Luminary("sun", 0.3, 0.0, 0.0, [earth]);
return sun;
}
var solarSystem = initSolarSystem();
function draw(obj) {
// Draw current object.
for (let key in obj.children)
if (obj.children.hasOwnProperty(key)) {
//if (typeof(obj.children) == "array") {
console.log(obj.children[key].radius);
draw(obj.children[key]);
}
}
draw(solarSystem);
本文标签: javascriptAccess child Object in jsStack Overflow
版权声明:本文标题:javascript - Access child Object in js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744033873a2579407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论