I'm trying to understand how the this
keyword works in this context.
function Person(name) {
this.name = name;
this.sayName = function() {
alert('My name is ' + this.name);
};
}
var p = new Person('Smith');
p.sayName(); // 'My name is Smith'
var q = p;
q.sayName(); // 'My name is Smith'
q = p.sayName;
q(); // 'My name is' ???
Why is the last example not picking up 'Smith'?
Is it because q is simply pointing to a function (i.e. the object the method belongs to is not recognized in this context)? Since q is in the global space, this
is global within the function (i.e. the caller of sayName() is the global space or window)?