What this does, is it prints out an alert that says [object Object]
my question is why is there a capital object along with a lowercase o object.
I’m wondering what the object is. and why doesn’t it print out foo since foo is the object. and when i run alert (this), it’s being run inside the foo object.
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].
As the name implies, it’s more restrictive than the default mode, to prevent some kinds of errors by changing the way JS behaves. The explanation over at MDN is pretty good and goes into detail.
The this keyword is a huge stumbling block for many JavaScript developers to the point that some avoid it altogether. The best description of the topic I’ve seen so far was by Yehuda Katz in his blog post on function invocation. When not using call/apply or bind, the this value will always refer to the global object, except in the following instances:
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.
Looking at your question, I believe you should take a step back and first get a better understanding of JavaScript objects, then worry about the this keyword. Everything in JavaScript is a property of an object, starting down from the global object.
Rather than calling the other mode “default” mode I’d call it “old mode”. All future versions of JavaScript will build on “strict mode” while retaining “old mode” simply so that antiquated scripts written for Netscape 4 do not break.
Strict mode fixes some of the problems that JavaScript has due to it now being used for far more than was originally intended.
As a simple example of something strict mode fixes is the requirement that everything be declared before use. This prevents the newbie error of accidental typos in variable names resulting in the script creating additional global variables instead of using the intended variable. Using old mode there is no easy way to tell why your 500 line JavaScript doesn’t do what it is supposed to be doing. Using strict mode the script fails to run and the error console takes you straight to the typo.