In JavaScript, the this context is not bound to every object method. Rather, it is defined at runtime as you call this method Check this answer to learn more about binding behavior. .
In your code, foo gets the getDate new Date property, which it gets from Date.prototype through the prototype chain. So your code is really equivalent:
var foo = Date.prototype.getDate; foo();
(Check this yourself: check in the console that (new Date).getDate === Date.prototype.getDate really true .)
Now, it should be clear that there is no actual context for this call. You can preset it manually using the bind function for the object. (Note: older browsers need shiv for Function.prototype.bind .)
var foo = Date.prototype.getDate.bind(new Date); foo();
Alternatively, set up the correct this context when you call / apply execute the function.
var foo = Date.prototype.getDate; foo.call(new Date);
source share