خرید بک لینک
Hi there :)

Ix92m trying to make a rather simple program (basically not much more than adding values), but Ix92m already stuck.

I'll try to explain: The idea is that there will be a x93main objectx94 with certain numerical values (that should be summed up into a variable - letx92s call it mainValue) and then any given number of x93sub objectsx94 (depending on how many the user wants to create) that each have numerical values themselves (summed up in var subValue). So the mainValue is the same for every sub object, but the subValue can vary of course. The sum of those two values gives my a total for each sub object (mainValue + subValue = totalValue).

To be able to loop through those sub objects, I wanx92t to put them into an array of objects inside the main object.

But how can I call the sub objects constructor in a way that coects a new x93sub objectx94 with its parent and pushes it into the array inside the x93main objectx94?

How and where can I call the sub objects constructor? Should it be placed inside or outside the main objectx92s constructor? Is both possible and if yes, is one preferred over the other and why?

I thought of something like this:

Code:

function MainObject(a,b,c){
        this.a = a;
        this.b = b;
        this.c = c;
        this.mainValue = a+b+c;

        this.arrayOfSubObjects = []
}

// should this be separate or go inside the MainObject constructor?
function SubObject(parent, x, y, z) {
        this.parent = parent;
        this.x = x;
        this.y = y;
        this.z = z;
        this.subValue = x+y+z;
}

MainObject.prototype = {
        //just an example, I would actually loop through the whole array
        buildTotal: function(i){
                var totalValue = (arrayOfSubObjects[i].subValue + mainValue);
                return totalValue;
        }
}

I hope Ix92m making sense. I'm really at a loss. All input is appreciated.

Thanks, thala

برچسب: نویسنده: آیلین رضوانی تاريخ: سه شنبه 14 شهريور 1396 ساعت: 18:46

صفحه بندی