Calling objects based on string type in javascript

I have the following code snippet where I want to invoke commands on different objects based on the type given.
How can I do this object oriented way in JavaScript.

function(reportTye){
	if( reportType == "REPORT_ONE"){
                   rep1Cmd = new ReportOne();
                   rep1Cmd .run();
          } else if( reportType ==  "REPORT_TWO"){
                 rep2Cmd = new ReportTwo();
		 rep2Cmd .run();
           } else if( reportType ==  "REPORT_THREE"){
                  rep3Cmd = new ReportThree();
		  rep3Cmd .run();
          }

Open in new window

ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
It wouldn't actually be that different to what you've got.  Why would you want it to be object oriented if you're not going to scale it?  (apologies if you are *grin*)

function Report(reportTye) {
  var types = {
    "REPORT_ONE": ReportOne,
    "REPORT_TWO": ReportTwo,
    "REPORT_THREE": ReportThree
  };

  var _reportTye = reportTye;

  this.run = function() {
    var t = new (types[_reportTye])();
    t.run();
  };
}

function ReportOne() {
  this.run = function() {
    alert('ReportOne');
  };
}

var r = new Report("REPORT_ONE");
r.run();

Open in new window

hieloCommented:
Is this what you are after?
<script>
function showReport(reportType){
	/*
    if( reportType == "REPORT_ONE"){
        rep1Cmd = new ReportOne();
        rep1Cmd .run();
    } else if( reportType ==  "REPORT_TWO"){
        rep2Cmd = new ReportTwo();
        rep2Cmd .run();
    } else if( reportType ==  "REPORT_THREE"){
        rep3Cmd = new ReportThree();
        rep3Cmd .run();
    }
	*/
	
	// see if there is a function defined that matches the name in reportType
    if( 'function'==typeof(window[reportType]) )
    {
        var rep = new window[reportType];
        rep.run();
    }
}

//this is your constructor
function ReportOne(){
	this.value=1;
}
//this is your 'run' public method
ReportOne.prototype.run=function(){
	alert( "Running... value=" + this.value);
};


//this is your constructor
function ReportTwo(){
	this.value="Hello";
}
//this is your 'run' public method
ReportTwo.prototype.run=function(){
	alert( "Running... value=" + this.value);
};


//this is your constructor
function ReportThree(){
	this.value="Bye";
}
//this is your 'run' public method
ReportThree.prototype.run=function(){
	alert( "Running... value=" + this.value);
};

/*
showReport("REPORT_ONE");
showReport("REPORT_TWO");
showReport("REPORT_THREE");
*/

// 
showReport("ReportOne");
showReport("ReportTwo");
showReport("ReportThree");

</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thank you guys.  Both are good solutions.  I was looking for something similar to Hielo
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.