1

Consider a class which contains a set of other objects.

function Food= (){ this.apples = 8; this.orange = 6; this.peach = 3; } 

Now, I can create an add function.

Food.prototype.add(food){ this.apple += food.apple; this.orange+=food.orange; this.peach+=food.peach; } 

However, what if I want to dynamically want to add in another thing into food, which food.prototype.add will also add?

The first question is, is there a simple way to add all the objects in the class?

The second question is this: I could have written the code as follows.

function Food = (){ this.apples = 8; this.orange = 6; this.peach = 3; this.addArray = []; this.addArray.push(this.apples); this.addArray.push(this.orange); this.addArray.push(this.peach); } Food.prototype.add(food){ for (i=0, i<this.addArray.length, i++){ this.addArray[i]+=food.addArray[i]; } } 

This way, if I want to add escargot to the things I want to add, I can simply add it to the addArray.

Is there a way I can make a similar addSet, or a addObject. So instead of an having an array with indexes, I can have a set or object with names instead?

2
  • To me it seems like you're conflating two different structures here. Your Food is actually Foods containing name and quantity pairs. You're on the right track, although your idea of using an object, rather than an array, seems like a lot better approach. (Here, by "a lot better approach", I mean the only one that makes any sense.) Commented Dec 12, 2016 at 4:47
  • You can iterate through an objects properties with for(var item in obj) { ... }, although based on what you are trying to do a data structure like an array would be a better idea. Mainly due to if you want to add any other properties to food that might not be a number you want to "add". Commented Dec 12, 2016 at 4:48

2 Answers 2

1

I think what you are looking for is

function Food() { this.apples = 8; this.orange = 6; this.peach = 3; this.addArray = ["apples", "orange", "peach"]; } Food.prototype.add = function(food) { for (var i=0, i<this.addArray.length, i++) { this[this.addArray[i]] += food[this.addArray[i]]; } }; 

However, I doubt this is a good idea if you are dynamically adding arbitrary fruits. As an easy way to extend your class, yes, but for anything more than that you better use a proper Map.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. I have a follow up question. If I want to use a map, I would change this.addArray to this.addMap = new Map(), and then this.addMap.set("apples", this.apples), etc. Is that right?
Nah, I meant you wouldn't have an .apples properties at all. A Food object would just contain a this.fruits = new Map([["apples", 8], ["orange", 6], ["peach", 3]]); and the add method would merge two maps.
1

Hey you can try this also...

Food.prototype.add(food){ for(var i in food) { if(this[i] != undefined) { this[i] += food[i]; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.