Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this JavaScript example point to the global object (Window)?

Tags:

javascript

function F() {
    function C() { 
        return this;
    } 
    return C();
} 
var o = new F();
like image 973
user353949 Avatar asked Jan 28 '26 08:01

user353949


1 Answers

Break down the component elements.

Suppose you were to do this:

function C() {
    return this;
}
var o = C();

There is clearly no object context here, so this is window.

Wrapping that setup in a constructor doesn't change the fact that there isn't any object involved in the context of a straightforward call to C().

like image 115
VoteyDisciple Avatar answered Jan 30 '26 21:01

VoteyDisciple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!