Skip to content

Latest commit

 

History

History

variable_scope

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Variable Scope

We have the following code:

var name = 'John',
    obj = {
        name: 'Mary',
        whoIam: function() {
            var name = 'James';

            console.log( this.name );

            setTimeout( function () {
                console.log( this.name );
            }, 100 );
        }
    };

obj.whoIam();

What does it prints?

"Mary"
undefined
"John"
__match_answer_and_solution__


Why?

It logs Mary because the context of execution is obj.
It logs John because setTimeout is executed in the global context.
__match_answer_and_solution__