Skip to content

Latest commit

 

History

History
48 lines (41 loc) · 651 Bytes

File metadata and controls

48 lines (41 loc) · 651 Bytes

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__