Now let's look at example see the one that says, What is this? So I've got this function foo again. And inside of foo, I create a variable. So this is good. I'm using the var keyword again, which I think is better. So I'm creating a private variable inside of foo.
So speed is private to this function. And I alert this start speed. Okay. And then I execute through so we should get an alert. But the question is, what will be the value that we see in the alert? That's the question.
So let's try it. Let's copy this code, and go to JavaScript console, paste that code in and when you execute the code we see undefined, huh? So the alerts, this dot speed is undefined. All right, let's see if we can figure out what's going on. Well, once again, we have this private variable speed, and we alert this dot speed. Well, when I see something, that's something I think object property.
So this thought speech is not the same thing as speed because if we wanted speed, we would just get speed. But we don't get speed, we get undefined. And we're not alerting speed. We're learning this dot speed. So, as we've talked about many times in this class, that JavaScript, this keyword refers to the object to which a method belongs. Well, this foo function isn't really a method, or is it?
Maybe it is. Let's copy this code again. And let's execute the code. And then let's see what happens if we type foo. Hmm. So if I just type foo in my console, I get looks like the code from our function, it looks exactly the same as the code from our function.
Well turns out that and if I type window dot foo, I get the code from our function. So it turns out the Foo is a property of the window object and The reason is because we're declaring foo in the global scope right here, we're in the global scope, we're not inside of a function where we're inside of a function here on line 26, or 28. But the actual function declaration of foo on line 25 is in the global scope. So when we create a global function, we're creating a global variable. And as we discussed a few minutes ago, a global variable is also a property of the window object. So right now foo is a property of the window object.
Okay? So that means that the this keyword refers to the window object. So when I say this dot speed, I'm asking for the speed property of the window object. So alert, this dot speed will do the exact same thing as saying alert window dot speed lines. 2829 are exactly the same. They're the exact same thing.
So the situation is we don't have a speed property on window. Now. In this example in the previous example we did but here we don't, because this speed is private. Right there in line 26, that's a private speed, but window dot speed doesn't exist. And if I were to come up here and say, var speed equals, let's say 5000. So copy this code, and refresh the page.
And I'm going to paste that code in. And now I see 5000. How do I see 5000? Well, the reason is because now we create a global variable called speed. This dot speed equals this.is window dot speed. So window dot speed, the global speed is 5000.
So in this case, we get a value which is which is good, but the original question did not involve a global speed variable. So in this case, on line 28, this dot speed or window dot speed is undefined. We never defined a window dot speed, and that means the alert on 28 produces undefined there is no window to be proper