[Laszlo-user] what does 'this' refer to inside a function?

Sarah Allen sallen at laszlosystems.com
Tue Jun 13 21:03:26 EDT 2006


ok, I just code reviewed a fix in scrollrichedittext that I don't really 
understand.  Perhaps one of the Javascript gurus on the list can 
enlighten us.

The following code generated a waring that inp was undefined:
this.applyConstraint("stepsize",
                         function() { this.setAttribute("stepsize", 
parent.inp.lineheight); },
                         [p.inp, "lineheight"]);

The bug was fixed by adding an explicit 'this' before parent.inp:
this.applyConstraint("stepsize",
                         function() { this.setAttribute("stepsize", 
this.parent.inp.lineheight); },
                         [p.inp, "lineheight"]);

My first thought was... I thought you never needed an explicit this for 
parent since it is defined at construct time.
My second thought was... um, what the heck is 'this' in the middle of an 
anonymous function?  I would have thought it was no longer in the scope 
of the object.  Why does this work?

Confused,
Sarah



More information about the Laszlo-user mailing list