/ Published in: JavaScript
Using an object as a simple switch.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var noSwitch = { "opt1": function(){ alert("opt1"); }, "opt2": function(){ alert("opt2"); }, "default": function(){ alert("default"); } } var foo; var foo = "opt1"; var foo = "opt2"; if(noSwitch[foo]){ noSwitch[foo](); } else { noSwitch['default'](); }
URL: http://jqfundamentals.com/book/index.html