0

I have this object

function paymentPlan(name, price, days, active){
        this.name=name;
        this.price=price;
        this.days=days;
        this.active=active;
    }

    var plan_1 = new paymentPlan("unlimited", 0, -1, -1);
    var plan_2 = new paymentPlan("starter", 100, 7, 2);

and I need to select one of the object instances by building the variable instance from another variable, essentially add the number to the end of the reference

for example If i needed to print out the name of plan_2 when selected, I need to build the reference like so

var plan_id = 2;

alert( plan_+plan_id.name ); 

how can I do that?

2
  • 1
    ? plan_id and plan_ are not in the object/code above, can you clarify your meaning? Commented Dec 13, 2012 at 22:05
  • Why not use an array instead, where the index is the plan id Commented Dec 14, 2012 at 1:02

4 Answers 4

4

Your best approach would be to store the plans in an array.

var plans = [];
plans.push(new paymentPlan("unlimited", 0, -1, -1));
plans.push(new paymentPlan("starter", 100, 7, 2));

var plan_id = 1;
alert(plans[plan_id].name); // "starter"
Sign up to request clarification or add additional context in comments.

4 Comments

oh yea!! why didn't I use an array, I'll use an array instead. Thanks for your help!!!!
@Aesthete, you might like to change plan_id = 2 to plan_id = 1 because plans[2] doesn't exist in your example as it stands.
@Beetroot-Beetroot - Indeed! Good spot. Fixed.
Well my example is a little different then what's in the code, I'm using PHP to spit out the javascript instances, and the number at the end of "plan_" is the id number of the plan in the database, and I needed to call that plan by it's ID number, so I don't even need "plan_" I can just use plan[2] instead. I made a bad choice for choosing an object. Also I was under the impression that you could only select one answer on stack overflow, thanks for the tip.
1

If you define your variables globally, then you may use window object:

window["plan_" + plan_id].name;

However, you may always initialise the variables inside a locally defined object, e.g.:

var plans = {
    plan_1 : new paymentPlan("unlimited", 0, -1, -1),
    plan_2 : new paymentPlan("starter", 100, 7, 2)
};

plans["plan_" + plan_id].name;

Comments

0

Use a parent object

var plans = {};
var plan_1 = new paymentPlan("unlimited", 0, -1, -1);
plans["plan_1"] = plan_1;
alert( plans["plan_"+plan_id.name] );

Comments

0

Try to use eval().

alert(eval('plan_'+plan_id+'.name'));

3 Comments

@MattiMehtonen, this is stereotype, if you sanitize data and 100% sure about what you are doing this is very handy tool. BW look at jQuery 'globalEval' they are using it ;)
@dmi3y. Ok then. (eval == evil) :)
This answer is the closest to what the OP asked for (although the other methods accomplish the same result). You could wrap it in if(!isNaN(plan_id)){} for safety.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.