-3

how to pass "this" to another function ???

function demo1()
    {

        demo2(this);
    }

how to get this object of demo1 into demo2 ???

function demo2()
{
     //how to get this object of demo1 into demo2 ??? 
}
Rahul Gupta
  • 7,933
  • 5
  • 49
  • 60
user3755867
  • 57
  • 11
  • 1
    Not sure if I got this correctly, but maybe you are looking for `demo2.call(this);` instead of `demo2(this);`. – Dennis Jul 11 '14 at 12:48

3 Answers3

-2

You can do this a number of ways.

Pass it in You're already doing this, sort of.

function demo1()
{
    demo2(this);
}

function demo2(demo1)
{
    console.log(demo1);
}

Call it in the current scope

function demo1()
{
    // Executes demo2 in scope of demo1
    demo2.call(this);
}

function demo2()
{
    console.log(this);
}
David Barker
  • 13,767
  • 3
  • 40
  • 73
-2
function demo1()
    {

        demo2(this);
    }

function demo2(data)
{
     console.log(data);
}

Check out the details on console.

-3

That's quite easy to do. You can use .call or .apply:

demo2.call(this);

The first argument you provide will be the context (this) used during execution.

skerit
  • 17,691
  • 22
  • 92
  • 138