6
HeroName = new Hero()
HeroName.Spells = [];
HeroName.Spells[0].Type = [];

This doesnt work =( , even if I try new Array() or anything else. Is it not possible to do arrays within arrays? This is what I was going for:

HeroName.Spells[0].Type[0] = new DmgSpell();
HeroName.Spells[0].Type[1] = new Buff();

I know I can do something like

HeroName.Spells[0][0] = new DmgSpelL();
HeroName.Spells[0][1] = new Buff();

But this doesn't make it as easy to read

Am I doing something wrong? I've tried every possible combination I could think of and using google to search 'array within an array' gives me other results that don't help me. Any help is greatly appreciated

2
  • 1
    Spells[0] needs to have an object assigned to it for that to work - at the moment it looks like you're trying to add a property Type to a nonexistant object. Commented Aug 29, 2013 at 1:41
  • Sorry for missing such an obvious thing and thank you so much for explaining it Commented Aug 29, 2013 at 1:45

2 Answers 2

7

You missed a step. You haven't declared HeroName.Spells[0] to be an object, so you can't a Type property to it, because it doesn't exist. This works:

HeroName = new Hero();
HeroName.Spells = [];
HeroName.Spells[0] = {};
HeroName.Spells[0].Type = [];
Sign up to request clarification or add additional context in comments.

1 Comment

Doh I feel stupid now...thanks so much for your help, so glad it works out! thank you once again
3

Set HeroName.Spells[0] as an Object, otherwise, it is undefined. undefined can't have any properties.

HeroName.Spells[0] = {};

1 Comment

Thank you very much for taking the time to explain and write out an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.