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 ???
}
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 ???
}
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);
}
function demo1()
{
demo2(this);
}
function demo2(data)
{
console.log(data);
}
Check out the details on console.
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.