Hello,
I hope one (or some) of the experts in this forum would chime-in on this thread to help clear my understanding.
I’ve created a jQuery script that uses a switch statement. However, my experience with it, relative to variable scope, doesn’t seem to follow the logic.
According to the JavaScript/jQuery theory, a global variable was accessible (meaning read & write) throughtout any function within any script (one that page).
However, apparently that theory wasn’t completely true as it pertained to switch statements that contained variables.
To illustrate my case in point, I’ve included a simplistic version of my code:
$(“#selector”).delegate(“div”, “click”, function(event) {
var testVar = 4;
switch (this.id) {
case "id1":
alert(testVar); message returns 4
do something
var testVar = 3;
alert(testVar); message returns 3
case “id2”:
alert(testVar); message returns 4 “yikes!”
}
});
As shown, the variable “testVar” is not accessible from one case to the next case .
This situation drove me nuts until I figured it out, which in my mind is stupid.
Am I missing something terribly important in my assessment here?
Furthermore, to add insult to injury, I am seeing the same behavior within the conditional if else statement counterpart to the switch statement.
As always, thank you for your comments.
Bryan