Extracted from jqfundamentals, here is an example:
var person = {
firstName : 'Boaz',
lastName : 'Sender',
greet : function() {
log( 'Hi, ' + this.firstName );
}
};
var sayIt = person.greet; // save method into a variable
sayIt(); // displays 'Hi, undefined' -- problem!
To clarify,
When we save the .greet() method in sayIt and then invoke sayIt(), the context changes to the global window object instead of the person object. Since the window object does not contain a firstName property, it returns undefined when accessed.
The issue at hand is
After saving the .greet() method in sayIt and calling sayIt(), why does the context shift to the global window object?