A JavaScript function is an object, and a method is an object property that contains a function. Object ancestor can assign a function to a property of object baby:
ancestor.makeSomethingForBaby = function(baby)
{
baby.doSomething = function()
{
.....something involving "this".
};
};
When [font=monospace]baby.doSomething()[/font] is called, [font=monospace]this[/font] will refer to baby, not ancestor. If you want baby to hold a reference to ancestor, it has to be through another variable.
ancestor.makeSomethingForBaby = function(baby)
{
var muggins_here = this;
baby.doSomething = function()
{
.....something involving "muggins_here".
};
};
When [font=monospace]baby.doSomething()[/font] is called, [font=monospace]muggins_here[/font] will refer to ancestor (and [font=monospace]this[/font] will still of course refer to baby).
This is called "late binding", to contrast it with "early binding" (which is what PHP would do in this situation) where [font=monospace]$this[/font] is identified with ("bound to") the object creating the function.