0

I've been looking for a special argument and/or variable that can do this i.e.:

function myFunction(arg1, arg2) {
 switch(arg2) {
 case FIRSTCHOICE:
 // Code
 break;
 case SECONDCHOICE:
 // Code
 break;
 }
}

and usage it as:

myFunction('String', FIRSTCHOICE);

So for instance I would make different type of alerts that output a message in different styles:

function myAlert(msg, type) {
 switch(type) {
 case STYLE_ONE:
 alert("STYLE ONE: " + msg);
 break;
 case STYLE_TWO:
 alert("STYLE TWO: *** " + msg + " ***");
 }
}

and use it as follow:

myAlert("Hello World", STYLE_ONE);

or

myAlert("Hello World, again", STYLE_TWO);

I know that you can't make switches that does that, and I've been looking around the internet for it but with no answer on what this type is called or if it is possible. Maybe there is a workaround?

Help is much appriciated.

Best Regards, Christian

asked Apr 29, 2011 at 13:45
2
  • Well,, you could always create a global variable var MYCONSTANT = 1; var MYCONSTANT2 = 2; and then refer to them in the switch and input args Commented Apr 29, 2011 at 13:48
  • 1
    Or better yet... var SOME_CONSTANT_TYPE = { style_one:1, style_two:2, style_three:3 }; then call myFunction('string',SOME_CONSTANT_TYPE.style_one) Commented Apr 29, 2011 at 13:51

4 Answers 4

1

I don't see what your specific problem is, here. Those look like regular consts, which could easily be created as global variables in javascript.

var STYLE_ONE = 1;
var STYLE_TWO = 2;
function myAlert(msg, type) {
 switch(type) {
 case STYLE_ONE:
 alert("STYLE ONE: " + msg);
 break;
 case STYLE_TWO:
 alert("STYLE TWO: *** " + msg + " ***");
 }
}
myAlert("test", STYLE_TWO);

Example

answered Apr 29, 2011 at 13:48
Sign up to request clarification or add additional context in comments.

Comments

1

TRY this:

function myAlert(msg, type) {
 switch(type) {
 case 'STYLE_ONE':
 alert("STYLE ONE: " + msg);
 break;
 case 'STYLE_TWO':
 alert("STYLE TWO: *** " + msg + " ***");
 }
}
myAlert("Hello World", 'STYLE_TWO');
answered Apr 29, 2011 at 13:48

Comments

0

what you want, is the javascript equivalent of an enum.

Basically, an enum is an integer with a name.

in javascript, this could be done as follows:

var enumObj = new Object();
enumObj.type = {help:1, about:2, quit:3}
document.write(enumObj.type.about);
 // outputs : 2
answered Apr 29, 2011 at 13:49

Comments

0
var SOME_CONSTANT_TYPE = { style_one:1, style_two:2, style_three:3 }; 

then call

myFunction('string',SOME_CONSTANT_TYPE.style_one)
answered Apr 29, 2011 at 13:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.