Browse files

Add comments explaining switch block

  • Loading branch information...
1 parent 54946db commit a9711df214b4aeef15708862a52b89369248faba @eddiemonge eddiemonge committed Oct 24, 2013
Showing with 3 additions and 2 deletions.
  1. +3 −2 page/javascript-101/conditional-code.md
View
5 page/javascript-101/conditional-code.md
@@ -115,12 +115,13 @@ var stuffToDo = {
};
+// Check if the property exists in the object.
if ( stuffToDo[ foo ] ) {
@chrisdoc7
chrisdoc7 added a line comment Oct 24, 2013

I insist that is necessary that the property foo has quotes:

if ( stuffToDo[ "foo" ] ) {

@scottgonzalez
jQuery Foundation member
scottgonzalez added a line comment Oct 24, 2013

No matter how much you insist, you will still be wrong. Please read the article again. This is an alternative implementation of a switch statement, where foo is the expression being checked and stuffToDo is the set of cases.

@kswedberg
jQuery Foundation member
kswedberg added a line comment Oct 24, 2013

@scottgonzalez is correct.
It might be clearer if the code explicitly stated in a comment at the top of the code block that foo is a variable defined elsewhere. Might help if a comment were added to the switch statement code block, too.

@chrisdoc7
chrisdoc7 added a line comment Oct 24, 2013

I now understand it. But i agree with @kswedberg if the code explicitly stated in a comment at the top. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
-
+ // This code won't run.
stuffToDo[ foo ]();
} else {
-
+ // This code will run.
stuffToDo[ "default" ]();
}

1 comment on commit a9711df

@scottgonzalez
jQuery Foundation member

This is actually a pretty terrible comment, as it doesn't actually clarify anything. There is no value assigned to foo in the example, so the execution path is unknown. If these comments were correct, the code would be pointless.

Please sign in to comment.