When TO and NOT TO USE "this." method in JS?


HI experts,

I encountered this haze while I was doing the “Advanced Objects - 6.” The instruction told me to check the “num” if is 0 and I used “this.num” instead. although I found out that I need to omit the “this.” to let me pass, but I’m still puzzled about when TO and when NOT TO use “this.” during coding.

please look at my code and the code that the hint told me to do.


this is used when we access properties (_model, _energyLevel) or methods within a method.

num is the parameter of the method, so we don’t have to use this.


In a function definition, this refers to the “owner” of the function.

the variable num is a parameter the function needs en is therefor not part of the object robot. This is why you dont use this on num.

I recommend reading the docs for more clarification on this subject.
MDN web docs


Given that setters act upon the instance variables of the current object context, their setting takes without needing a return.

 > const foobar = {
     _foo: 'bar',
     get foo() {
       return this._foo;
     set foo(bar) {
       this._foo = bar;
<- undefined
 > foobar.foo = 'baz'
<- "baz"