Switch using an Object


/ Published in: JavaScript

Using an object as a simple switch.


Copy this code and paste it in your HTML
  1. var noSwitch = {
  2. "opt1": function(){
  3. alert("opt1");
  4. },
  5. "opt2": function(){
  6. alert("opt2");
  7. },
  8. "default": function(){
  9. alert("default");
  10. }
  11. }
  12.  
  13. var foo;
  14. var foo = "opt1";
  15. var foo = "opt2";
  16.  
  17. if(noSwitch[foo]){
  18. noSwitch[foo]();
  19. } else {
  20. noSwitch['default']();
  21. }

URL: http://jqfundamentals.com/book/index.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.