When you call a function as a property of an object, for example obj.func() , this refers to obj .
This is exactly what you are doing here. arr is your object, and 0 is a property containing the function.
Note: After all, arrays are just objects, and their elements are the values โโof their properties (although properties are usually numeric strings (all properties are strings)).
For more details see MDN - this , in this case:
When a function is called as a method of an object, its this set to the object on which the method is called.
In your second case, you call the function "autonomously", so this refers to window . If the code was run in strict mode, this would be undefined .
Felix kling
source share