Re:Advanced Javascript tips and tricks
by IanDrake on Tuesday July 05, 2011 @10:51AM
(#2730015)
I use the pattern listed under "Objects private and public members" frequently and feel they missed an important point.
They really should add something like:
function MyConstructor( pub, priv ) {
var thisMyConstructor = this; //**Copy the 'this' variable**
var privateVariable = priv;
this.publicVariable = pub;
Otherwise "public" variables can't be accessed in private functions because "this" no longer references the object. So, from what I've learned it's always best to copy "this" to a private variable and use the private variable for consistency.
They really should add something like:
function MyConstructor( pub, priv ) {
Otherwise "public" variables can't be accessed in private functions because "this" no longer references the object. So, from what I've learned it's always best to copy "this" to a private variable and use the private variable for consistency.