Reason why we would write a code this way


I'm curious on why this code is written the way it is. From my understanding the methods are for reuse which is why we use the "this" [Ojbectname]. But since we are declaring the function square.calcPermieter doesn't that mean it's only ever going to be used for the squad Object?

I've tested this code out by replacing "this" with the actual Objectname and it still works.

Replace this line with your code. 
var square = new Object();
square.sideLength = 6;

square.calcPerimeter = function() {
  return this.sideLength * 4;
// help us define an area method here
square.calcArea = function() {
 return this.sideLength * this.sideLength;   

var p = square.calcPerimeter();
var a = square.calcArea();


square is the object and calcPerimeter and calcArea are two of the square object's methods.

this. is a an instance of a current object in the class method. :slight_smile:


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