Resetting property value without assignment?

Resetting property value without assignment?
0

#1

I’m studying this code from this guide and I don’t understand how the clearer() function works on line 223.

  clearer: function(){
      $("#display").text(0);
      this.state = states.START;
      this.op;
      this.disp;
      this.acc1;
      this.acc2;
},

If you’ve already pressed some buttons on the calculator and those variables have changed, wouldn’t you have to assign them with an empty string, like this.acc1 = "";? How does just the property declaration clear the value?

Thanks for any help!


#2

It doesn’t. Just writing the property in a statement doesn’t do anything. Here’s a quick demo.

But it looks like there’s more going on in the calculator code such that there’s no need to clear those values.


#3

It isn’t very nice code that you’re reading - there are much better ways to structure it, I think.

I’m guessing he forgot to clear those variables but it doesn’t matter because it doesn’t do anything until they’ve been over-written anyway (he got ‘lucky’) - he’s clearing the display text on the screen manually but not the underlying variable.

Made me feel better about my own shitty code, anyway :slight_smile:


#4

Yea you’re right. It’s look like even if he hit the clear button on his calculator, the old values would still be in acc1 and acc2 and would be reassigned when he pressed new numbers.

Thank you :slight_smile: !!!


#5

I ran my own tests and thought maybe there was something I didn’t understand about the language…

Thank you for the example, that’s reassuring of my own knowledge :).