1

I am trying to understand the key word this in JavaScript, it's a bit confusing.

I saw two ways of using this, which I am not entirely sure to understand. But, here is what I understood, correct me if it is wrong.

  1. First way to use this:

    var setAge = function (newAge) {
      this.age = newAge;
    };
    // now we make bob
    var bob = new Object();
    bob.age = 30;
    // and down here we just use the method we already made
    bob.setAge = setAge;
    
    // change bob's age to 50 here
    bob.setAge(50);
    

    What I get from this one that this is being used as global, like you can change the age to whatever you want.

  2. Second way to use this:

    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();
    

    I am not sure about this one, please explain to me... as my brain is trying to make sense of this...

perror
  • 7,071
  • 16
  • 58
  • 85
Munkiuke
  • 57
  • 5
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this – SLaks Dec 25 '15 at 19:54
  • http://www.quirksmode.org/js/this.html – Pabs123 Dec 25 '15 at 19:57
  • 1
    `obj.method(argument)` is doing the same thing as `obj.method.call(obj, argument)`. The object itself is passed as the special `this` argument into the function. You could also call `this` the zeroth argument to a function. An object can be imagined as a dictionary with some entries in there. Such an entry can for example be a function or a number or some other value. – Tesseract Dec 25 '15 at 19:59
  • Btw, it's **javascript**, not java script. – Joaquín O Dec 25 '15 at 20:22
  • You likely need to do some reading about how `this` works in Javascript. I'd suggest these answers: [When you pass 'this' as an argument](http://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676) and [Function defined with 'this', but executing without 'this'](http://stackoverflow.com/questions/12042946/function-defined-with-this-but-executing-without-this/12043026#12043026). – jfriend00 Dec 25 '15 at 20:27

1 Answers1

0

Let me show you what is happening in both of the codes:

First:

var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;

// change bob's age to 50 here
bob.setAge(50);

Basically this.age = newAge is doing is that when he says Bob.setAge , you can change the actual age of the Object. Its like a setter. Getters and Setters are more explained here:

Second 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();

Here when it says return this.sideLength, it is getting the sideLength of the square that is defined in that Object. You cane just say sideLength since it is not a global variable. This is explained more thoroughly here

Good Job Keep using Codecademy, probably the best place to start of learning to code!