there is two type of scope in javascript named function scope global scope
now i am executing this code
function abc()
{
alert(this);
}
abc();
abc call returning me [object Window] Why?? function makes another scope so why it is representing window
this
, inside any function, will be the object on which the function is invoked. In your case, you are not invoking it on any object. So, by defaultthis
refer toglobal
object, in your browser, it is thewindow
object.But in
strict
mode, if you invoke it like this,this
will beundefined
.Or
The
this
keyword refers to the object the function belongs to, or the window object if the function belongs to no object.Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Your function is under global(window) object. I mean,
You can write your function under custom object