0

These are from the knockoutjs.com tutorials.

Can anyone explain what the "this" at the end of the fullName property does? Please mention the JS principle at work here so that I may read about further, thanks!

enter image description here

Why does the totalSurcharge property not need the "this" at the end?

enter image description here

nanonerd
  • 1,964
  • 6
  • 23
  • 49

1 Answers1

3

The second argument to a ko.computed sets the value of this when the function to determine the computed's value (the first arg) is executed. In the fullName case, the function uses this.firstName and this.lastName. So, whenever it is called we want to make sure that this is indeed our viewmodel.

In the second case, a variable called self was created that points to the appropriate value of this. Then, self is used inside the computed's function rather than using this (which is dynamic). In the second case, self could have been put as the second argument and then this could have been used inside the function.

This is really a matter of style. In my opinion, the use of a variable like self has fallen out of style these days. In the end, it comes down to personal preference.

Here is another answer that discusses this in KO as well: Difference between knockout View Models declared as object literals vs functions

Good tutorial on this in JavaScript here: https://derickbailey.com/email-courses/masteringthis/

Community
  • 1
  • 1
RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211