2

I want to concatenate the contents of one object with another, but within the same list/command:

var MyVar = {
    options: {
        option1: "foo",
        option2: "bar",
        option3: "It's not kung "+options.option1+", but kung fu",
    }
}

this doesn't work, any hints on what I'm doing wrong here?

Ewout
  • 2,348
  • 1
  • 20
  • 24

1 Answers1

1

options.option1 is not defined yet at the point you are using it.

var MyVar = {
  options: {
    option1: "foo",
    option2: "bar"
  }
};

MyVar.options.option3 = "It's not kung "+MyVar.options.option1+", but kung fu";

should work as intended.

Note that this is not dynamic (option3 does not change on changes on option1). If you want dynamic behaviour, you find solutions here: Self-references in object literal declarations

Community
  • 1
  • 1
Alexander Presber
  • 6,429
  • 2
  • 37
  • 66