I have this code:
function boo() {
this.is_global = "Yes!";
}
When I run the boo();, I have is_global in window object, I mean the is_global goes into global context.
I expected to have is_global in boo function only.
Is this a normal behavior?
thisrefers to the window. If you want to refer to the function, use:Now:
An alternative way would be to simply refer to boo by its own name when adding properties to it.
Note that this modifies the original
boofunction object. If you wish to modify all instances ofbooresulting from using it as a constructor instead, please see the other answers here.