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

4 Answers 4

7

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().

Sign up to request clarification or add additional context in comments.

Comments

2

function C() is not a method of F, what you need to do is something like this:

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

Although that is a bit convoluted, when you could just do this to achieve the same thing:

function F() {}
var o = new F();

Comments

0

C() is not a method of the f object. As in, you can't call o.C();. If that makes sense. and because you return the return value of C() instead of a new instance of C it returns the window object.

Comments

0

function F() { return this; } will also return window. So will var obj = this. It't the value of "this" whenever "this" has no other value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.