Shall one use the "this" keyword when accessing object-properties from within the object which has the property.
I've made this demo. Both way worked fine.
So, is there a good reason to add the "this" or can one leave it out?
function CalcCircle(radius) {
this.radius = radius;
this.getCircle = function () {
// Accessing the property "radius" WITH "this".
return (Math.PI * this.radius * 2);
};
}
// Without the "this" keyword.
function CalcCircle2(radius) {
this.radius = radius;
this.getCircle = function () {
// Accessing the property "radius" WITHOUT "this".
return (Math.PI * radius * 2);
};
}
var o1 = new CalcCircle(10);
var o2 = new CalcCircle2(10);
console.log('With "this": ' + o1.getCircle());
// With "this": 62.83185307179586
console.log('Without "this": ' + o2.getCircle());
// Without "this": 62.83185307179586