Skip to content

Update conditional-code.md #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Update conditional-code.md #546

wants to merge 1 commit into from

Conversation

yatinmistry
Copy link

String parameter are not parsed to object

// Check if the property exists in the object.
if ( stuffToDo[ foo ] ) {
// This code won't run.
stuffToDo[ foo ]();
} else {
// This code will run.
stuffToDo[ "default" ]();
}

@mgol
Copy link
Member

mgol commented Aug 7, 2014

String parameter are not parsed to object

I don't understand what you mean, can you elaborate?

@yatinmistry
Copy link
Author

When i run this code i gives me error.
ReferenceError: foo is not defined
I passed it as string then it works. if something i have done wrong to execute then let me know.

    var stuffToDo = {
        "bar": function() {
            alert( "the value was bar -- yay!" );
        },
        "baz": function() {
            alert( "boo baz :(" );
        },
        "default": function() {
            alert( "everything else is just ok" );
        }
    };
    // Check if the property exists in the object.
    if ( stuffToDo[ foo ] ) {

// This code won't run.
stuffToDo foo ;
} else {
// This code will run.
stuffToDo "default" ;
}

@yatinmistry
Copy link
Author

You can run this script. It will gives errror.

@mgol
Copy link
Member

mgol commented Aug 9, 2014

You need to assign something to the variable foo before this code. The code with "foo" instead of foo wouldn't make sense, it would never go inside the if block.

@mgol mgol closed this Aug 9, 2014
@mgol
Copy link
Member

mgol commented Aug 9, 2014

I see what can be causing confusion here. The comments This code won't run. and This code will run. suggest the value of foo is already known which is not true (and even then, such comments wouldn't appear in the code). This is confusing people, there have already been a couple of issues/PRs/comments related to that.

@scottgonzalez I found the comments were added in commit a9711df and have been kept for a couple of months already. This commit doesn't make any sense, can we just revert it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants