GoodParts 1 this
by Joe LeMonnier
JavaScript
var a=2;
function add(c,d){
var e=c+d;
return e ;
}
var b= add(2,a);
document.writeln(b);
var myObject = {
value: 1,
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
document.writeln('this.value= '+this.value); // 1
}
};
myObject.double = function ( ) {
var that = this; // Workaround.
document.writeln('<br>1This.value=' + this.value);
document.writeln('<br>2this=' + this);
document.writeln('<br>3That.value=' + that.value);
var helper = function ( ) {
document.writeln('<br>4this.value=' + this.value);
document.writeln('<br>5that.value=' + that.value);
that.value = add(that.value, that.value);
document.writeln('<br>6That.value=' + that.value);
};
helper( ); // Invoke helper as a function.
};
// Invoke double as a method.
myObject.double( );
document.writeln('<br>7myObject.value=' + myObject.value);r