Open Sdcrouse opened 4 years ago
Just to be absolutely clear, the strange thing is that regardless of whether you write this:
let contextReturner = function() {
return this
}
Or this:
function contextRunner() {
return this
}
contextRunner()
always returns the window
object. But window.contextRunner()
only works in the latter case.
I also tried this out in NodeJS. This was the output:
> let contextReturner = function() {
... return this
... }
> contextReturner()
//=> global
> contextReturner() === global
true
> global.contextReturner
undefined
> global.contextReturner();
Uncaught TypeError: global.contextReturner is not a function
> function test() {
... return this
... }
> test()
//=> global
> global.test()
//=> global
Evidently, NodeJS has been changed since this lesson's last commit as well.
In the Access Implicitly-Set Global Object in a Function Expression section of the README, they give us this function:
So far, so good. That works in the console. But then the README says this:
Strangely enough, when I tried this out in the Chrome console, this was the output:
In the Microsoft Edge console, this was the output:
I don't know why this is, though. If the value of
this
returned bycontextReturner
is thewindow
object, then why doesn't the console recognize that thecontextReturner
function was defined on thewindow
object?One thing that I will also point out is that if you define a similar function without setting it to a variable like this:
Then, it behaves as expected in the Chrome console (and similarly in Edge):
I encountered a similar issue with the
locationReturner
function further down in the README.I'm not entirely sure what's going on here. Somehow, a function only gets defined on the
window
object if you don't store that function in a variable. It may be that the browser consoles have changed since the last commit was made on this lesson.