How to solve huge hierarchy in JavaScript function? With Switch? Thank you.
(Sorry, I am not native English speaker.)
function do(param1, param2) {
switch(param1) {
case "write":
/* another switch for param2 */
break;
...
}
}
Example calls:
do('write','house'); // write house
do('return','house'); // return house
etc.
Create an object that names functions the way you want:
You could write your function to simply look up the members in that object using array notation:
Doing it this way would be much easier to maintain and would be much more scalable. However, I wouldn’t even do it that way. I’d replace the do function with the object itself and then you could simply call these members the old fashioned way:
Or, if the name return causes problems and you really want to use it:
There’s alot of power and flexibility in JavaScript objects and functions.