-2

I am trying to initialize an object and and assign a property of its own to one of the properties.
But my syntax is incorrect.i am referring to the following line:

PCMACUrl =  genericURL + "/test" 

i have tried

testList[0] = {
    executionTimeSec:60,
    genericURL:"www.gmail.com",
    comments: "",
    PCMACUrl = genericURL   + "/test"
};
axelduch
  • 10,769
  • 2
  • 31
  • 50
  • @Amberlamps you probably mean `:` – axelduch Nov 10 '14 at 12:29
  • @Amberlamps The question is about a reference to self in a declaration of an object, not about a typo (ie. `=` -> `:`) – axelduch Nov 10 '14 at 12:31
  • @aduch I voted to close. Seems to me they have typed the syntax correctly aside from that last line, so they should know how to create object properties. Either way, it's a pointless question of use to no-one. – CodingIntrigue Nov 10 '14 at 12:33
  • @RGraham Fair enough – axelduch Nov 10 '14 at 12:34
  • i did it : PCMACUrl: genericURL + "/test". but i get an error in chrome console : genericURL is not defined. i just don't know javascript syntax. – user3649212 Nov 10 '14 at 12:39
  • Asked and answered dozens of times. Basic answer is you can't do this. –  Nov 10 '14 at 12:40
  • You can do it - using arrays/objects as variables (and manipulate those) or - if you like to walk on the edges - use the "evil" eval function :) – nettutvikler Nov 10 '14 at 16:19

1 Answers1

0

After re-reading all together I believe this is what you are looking for: (added after init, yes, I know, but it's clean and simple :)

var data = {
        'PropA': 1,
        'PropB': 2,
        'PropC': 3
    };
    data.PropD = data.PropC+5;
    console.log(data); //Object {PropA: 1, PropB: 2, PropC: 3, PropD: 8} 

Or, another way to look at it:

  • if it is possible use backend to generate the object you would like

  • probably you could also just get rid of same data in same object and use it differently when you call it (referencing to first object propertie and adding second on-the-go: .../ = data.PropC+anotherData.PropA /...

nettutvikler
  • 584
  • 7
  • 18
  • I'm not sure how this relates to the question at all. Could you please provide a code snippet that actually instantiates an object? – Bergi Nov 10 '14 at 17:07