user1931754
user1931754

Reputation:

javascript: this keyword

I know this points to current object on which function operates. So here is the code as per the definition

function foo(){
    alert(this); //output==window
}

So, now function foo is equal to window.foo() but now here

function foo(){
    function fo(){
        alert(this);
    }
    fo();
}

so,now when foo gets executed output is again window object why? since the nested this should refer to different object.since fo() is now not operating on window object as foo()==window.foo() .so nested function should now point to different object

see here for detail:

 function foo()
{
alert(this);
  function fo(){alert(this);}
as();
}

if now,var x=new foo();than "this" within the foo() method points to object object but the nested this points to global object right?now u should be clear what i meant to say

Upvotes: 2

Views: 214

Answers (4)

Tschallacka
Tschallacka

Reputation: 28722

Programming, you're doing it wrong.

To make fo an function of the object you should do this:

function foo(){
    this.fo = function(){
       alert(this);
    }
}
f = new foo();
f.fo();

see how the fo function is initiated in the object declaration?

Now when you do f.fo() you get [object Object]

Upvotes: 0

Andreas Louv
Andreas Louv

Reputation: 47099

Two things....

First of all you should look into using the console. (console.log(this);).

Second there is a difference between a closure and a scope (this).

Closure:

function a() {
    function b() {
        // b is only available inside `a`
    } 
}
// so out here b will be undefined

Scope:

function a() {
    // in here the scope can be multiply things depending on how the function is called. See below
    console.log(this);
}

Scope is per default window, if the function is a method of an object the scope refers to the object.

a(); // logs: window

var o = { 
    a: a
};
o.a(); // logs: { a: a }

You can overwrite this defalt behaviors by using ether call or apply

var s = [1, 2, 3]
a.call(s); // logs: [1, 2, 3]
// or using apply
a.apply(s) // logs: [1, 2, 3]

Upvotes: 2

Pulkit Goyal
Pulkit Goyal

Reputation: 5654

As explained here, the keyword this is bound dynamically to the object found to the left of the ‘.’ at call time. There are three exceptions to the above.

  1. When there is no . the keyword this is bound to the global object window.
  2. When you use call and apply, you get to decide what this is bound to.
  3. When you use the keyword new to create a new instance from a constructor, the keyword this refers to the newly generated instance.

Since here, you are still calling just fo(), this is bound to window.

Upvotes: 5

pimvdb
pimvdb

Reputation: 154848

The this value depends on how the function is called, not how it is defined. If you have the following function:

var test = function() {
  console.log(this);
};

Then there are numerous ways to call it:

  • test() - it will be window
  • new test() - it will be the instance
  • ({ foo: test }).foo() - it will be the object
  • test.call(bar) - it will be bar

Whether a function is nested or not doesn't matter.

Upvotes: 1

Related Questions