No, it doesn’t alert “[object]”, it alerts “[object Window]”, for the first snippet. It only alerts [object] if you’re using older IE versions. You shouldn’t.
It’s the same with alert(this.toString()). The toString() method is called automatically whenever the object is referred in string like manner, which alert(this) does.
The general description of toString() for objects is [object type]. In the first case, Window is the type of the global object: [object Window]. In the second case, Object is the type for the foo object: [object Object].
The function in question was called with the new operator, in which case this points to a new object being constructed.
The function in question is a member of an object, in which case this points to the object.
Rule 2 should be disregarded whenever the function involved is being called asynchronously, such as in a click handler or setTimeout. For example:
Person.getName(); // 'this' points to Person
setTimeout(Person.getName, 1000); // 'this' points to the global object
I’m not sure I get it right, I may be wrong, you’re phrasing may be off too… to note, the this keyword will only work inside functions, and will only be used to reference an object, not a property. But you can use this to reference the object, and then use “.”, like for any other named object, to look for a property of that object.