-1

I am trying to understand JavaScript functions. just wanted to know how the value of console.log(go()); returns 6?

var x = 10;
var foo = {
    x: 2,
    baz: {
        x : 1,
        bar: function() {
            return this.x + 1;
        }
    }
}    
var go = foo.baz.bar    
console.log(go()); //returns 6
console.log(foo.baz.bar()); //returns 2

can anyone pleas explain me how its returning 6?

11
  • 1
    In my browser the go() returns NaN. In what environment are you testing? Commented Jun 22, 2016 at 8:45
  • 1
    you have not assign the value to x which is first time undefined so it will return NaN not 6. can you please verify again. Commented Jun 22, 2016 at 8:50
  • this will return 6 only you have a context where x has value equal with 5,in your example you should have somewhere defined a global variable x = 5 Commented Jun 22, 2016 at 8:54
  • sorry guys, it returns NaN for me also. may be it was cache issue earlier. if i assign some value to x in the top, for ex: var x = 10, now it returns '11', in the case of 'console.log(foo.baz.bar());' it returns - 2, how is it happening? Commented Jun 22, 2016 at 8:58
  • @IamSarav In the first case (go()) this refers to the Window, so it's using the global variable x thats defined. In the second case (foo.baz.bar()) it's referring to x in the scope of foo.baz. That's why it's giving two different values. Commented Jun 22, 2016 at 9:02

3 Answers 3

2

console.log(foo.baz.bar()); will returns 2 after running the below code:

    var foo = {
    x: 2,
    baz: {
        x : 1,
        bar: function() {
            return this.x + 1;
        }
    }
}    
var go = foo.baz.bar    
console.log(foo.baz.bar()); //returns 2

But console.log(go()); is returning Nan in Firefox browser.

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

1 Comment

i apologies for it, it was cache issue for me earlier.
2

this in a function of JavaScript represents different objects when the function is called in different ways.

When you write foo.baz.bar(), this represents the last object in the calling object chain, that is the baz object.

However, in the following code, this doesn't represent the baz object as the final function call is not using the baz object. It belongs to the global object.

var go = foo.baz.bar;
go();

Somewhere in your code, x is a global object valued as 5, hence this.x + 1 would return 6.

Comments

1

This is due to the context and scope of this for both of these function calls. In the first case:

var go = foo.baz.bar;
go();

We are simply making go be the function that foo.baz.bar is, although it is only that function. You could think of it as just:

function() {
    return this.x + 1;
} 

Because of that this is referring to the Window, which is using the global variable x for this.x. Which is 5, giving the result 6.

In the second case we are indeed in the scope of foo.baz when we are accessing the function:

foo.baz.bar() 

In this case this.x refers to x defined in foo.baz which is 1, giving the result 2.

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.