1

this is one of my first questions to ask on this website so please do tell me if I am approaching this the wrong way.

Anyways, in my obj i've created (dog), I have a "live" function that counts the age of my dog starting from age (5) by using the setInterval feature of javascript. So calling the the function by typing "dog.live()" will proceed to perform the "if" side of the function until the dog's age gets to 15, then it will proceed to perform the "else" part of the function by telling you how long the dog has been dead for.

var dog = {
  age : 5, 
  live : function() {

    setInterval(function() {
      var timer = this.age += 1;

      if (this.age < 15) {
        console.log("Dog lives another day at age " + this.age);
      } else {
        console.log("Dog has been dead for " + (timer - 15) + " years");
      }
    }.bind(dog), 1000);
  }
}

dog.live();

"Dog lives another day at age 6"
"Dog lives another day at age 7"
...
"Dog has been dead for 0 years"
"Dog has been dead for 1 years"

Now, I understand how, at the "else" part of the function inside the setInterval, it increments the age by using the "timer" variable. However, I am stumped at how the "if" part of the function which tells how old the dog is, increments itself? As far as I can tell, it would not increment by itself because "var timer = this.age += 1;" is just declaring a variable and it's not performing anything itself. I would like to know how the dog's age get incremented while it is performing the "if" side of the function.

Again, if i'm asking this question the wrong way or if a question like this isn't permitted, please feel free to let me know and i'll further try and improve my future questions, thank you!

yunjae123
  • 29
  • 4
  • 1
    `+=` increments the variable. It's confusing because there's an additional `=` in that line… – deceze Dec 02 '16 at 10:13
  • Hmm I wish I understood what you meant by += incrementing the variable.. Unfortunately i'm still a little lost. – yunjae123 Dec 02 '16 at 10:16

1 Answers1

2

The increment is a little confusing because it happens inline. I'll simplify:

// Version 1
var timer = this.age += 1;

// Simpler 1
this.age += 1; // Could also use ++this.age; or this.age++;
var timer = this.age;

// Simpler 2
this.age = this.age + 1;
var timer = this.age;

The first line in each is just adding 1 to this.age in different ways.

The above code snippets all do the same thing, the first is just a "clever" way of writing it. It works because the evaluation order, so this.age += 1 is evaluated first, which means the correct value is stored in this.age, which then gets used by timer.

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66
  • Just to clarify, the assignment operator `=` actually returns the assigned value, so it itself can be used as an expression. – Luan Nico Dec 02 '16 at 10:18
  • Ahhh I think I'm slowly starting to understand that. I think it's my first time seeing something after "var" perform without the "var" being called. Thank you so much for the examples, that really cleared up the "clever" way of writing it. – yunjae123 Dec 02 '16 at 10:19