KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have some code like this: if (condition) { var variable = blah; } if (differentcondition) { var variable = blah; } Is this correct? I assume that the variable wouldn't be assigned if the condition doesn't return true. JSLint keeps on telling me, variable already defined. Am I doing this wrong? Thanks. OK, Here's my actual usecase, i'm doing event delegation like this: $("#container").click(function (event){ if ($(event.target).is('img.class1')) { var imagesrc = $(event.target).attr('src'); // Do something with imagesrc } if ($(event.target).is('img.class2')) { var imagesrc = $(event.target).attr('src'); // Do something with imagesrc } // This condition is mutually exclusive to the above 2 if ($(event.target).is('img.class3')) { var imagesrc = $(event.target).attr('src'); // Do something with imagesrc } // This condition is mutually exclusive to 1 and 2 but not to 3 if ($(event.target).is('img.class4')) { var imagesrc = $(event.target).attr('src'); // Do something with imagesrc } }); Actually these 2 classes are not mutually exclusive. This works for me, but is it correct? The answers were very informative, but I still don't understand how I should set up the variables here. Actually I also want to say that certain conditions are mutually exclusive, and certain conditions are not. How should I structure this? I probably should've used this example from the beginning.
Tags (comma-separated)
Save Edits
Cancel