33 Methods - is "this" required?


#1



With regards to what is expected when referencing an object's properties made through the use of the Circle constructor, does the "this" reference need to be included within the method for each property?


// option A
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
    };
    this.perimeter = function () {
        return 2 * Math.PI * this.radius;
    };
};

// option B
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * radius * radius;
    };
    this.perimeter = function () {
        return 2 * Math.PI * radius;
    };
};


#2

If you want to read or modify something in the object, then you'll need a reference to the object


#3

Hi,

Thanks for the reply. That said, I'm still unclear as to whether the "this" reference is required in the return statements.

// option A
function Circle (radius) {
this.area = function () {
return Math.PI * this.radius * this.radius;
};
};

// option B
function Circle (radius) {
this.area = function () {
return Math.PI * radius * radius;
};
};


#4

Required? Absolutely not. It's up to you to write instructions that add up to your desired result.
So, what value exists where and what do you want to do with it?

Do you have a reason for wanting to use this, or some reason to think it should be used in a certain way? If you're not certain, then look it up. google "mdn this"


#5

can u give us what is the idea ?

am confused a bit


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.