Data context for computed observable function in Knockout.js

CodeFarmer

I am comparing two ways of using computed observable function in Knockout.js

Question:

  1. Can "this" keyword in function refers to it's parent object (outside, not function inside)?
  2. Why version 2 works even without putting the context value at the end ?

        ///Version 1
        my.Product = function () {
            this.id = ko.observable();
            this.salePrice = ko.observable();
            this.photo = ko.observable();
            this.shortDescription = ko.observable();
            this.photoUrl = ko.computed
            (function () {
                return photoPath + this.photo();
            },this);  //**context**
        };
    
        ////version 2
        my.Product = function () {
            var self = this;
            self.id = ko.observable();
            self.salePrice = ko.observable();
            self.photo = ko.observable();
            self.shortDescription = ko.observable();
            self.photoUrl = ko.computed(function () {
                return photoPath + self.photo();
            });//why there is no "self" here
        };
    
mael

I am not sure I understand your 1st question. In the first case, you're passing "this" to be able to refer to "this" as "this" in your computed function. It is specific to knockout, but knockout probably uses call or apply. Both allow to redefine "this". If you did not pass "this" as the second parameter "this" would refer to the computed function scope. And this.photo would be undefined.

The second case is a common javascript "trick". It consists in assigning "this" to a variable to be able to refer to it in another scope. You do not have to pass "self" as a parameter because it is a variable and it is accessible from the function.

Both examples are valid. I personally prefer the second one because it is a more usual way of dealing with javascript execution contexts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Knockout pushing observable and computed data to an observable array

From Dev

Knockout.js: Subscribe or computed observable?

From Dev

Knockout.js: computed observable not updating as expected

From Dev

knockout.js observable groups/computed groups

From Dev

Knockout JS: Computed observable failing to update

From Dev

How to make 'independent' computed observable of some observable (Knockout.js)

From Dev

Knockout Computed Observable with parameters

From Dev

knockout observable array computed

From Dev

Running a function every time a computed observable changes Knockout

From Dev

How to use index in computed observable [knockout.js]

From Dev

Knockout JS Computed Observable also takes User input

From Dev

observable and computed are not working properly Knockout

From Dev

Knockout computed observable with access to observableArray

From Dev

Inheritance and overriding Knockout computed observable

From Dev

Inheritance and overriding Knockout computed observable

From Dev

knockout computed array not updating when function updates observable array used in computed

From Dev

Knockout/JS Referencing Observable In Other Function

From Dev

Cant get data to observable in knockout js

From Dev

Knockout computed not updating using observable array

From Dev

Knockout : find out which observable triggerred computed

From Dev

Knockout chained computed observable throws exception on update

From Dev

Knockout computed creates dependency for observable that is written to but not read

From Dev

Knockout Js computed not working in a model

From Dev

Knockout js Computed not being fired

From Dev

Knockout js Computed not being fired

From Dev

Practical Example using computed observable with another context

From Dev

Second computed function causes error in Knockout code

From Dev

Asynchronous call inside a Knockout computed function

From Dev

Knockout js computed running balance infinite loops

Related Related

  1. 1

    Knockout pushing observable and computed data to an observable array

  2. 2

    Knockout.js: Subscribe or computed observable?

  3. 3

    Knockout.js: computed observable not updating as expected

  4. 4

    knockout.js observable groups/computed groups

  5. 5

    Knockout JS: Computed observable failing to update

  6. 6

    How to make 'independent' computed observable of some observable (Knockout.js)

  7. 7

    Knockout Computed Observable with parameters

  8. 8

    knockout observable array computed

  9. 9

    Running a function every time a computed observable changes Knockout

  10. 10

    How to use index in computed observable [knockout.js]

  11. 11

    Knockout JS Computed Observable also takes User input

  12. 12

    observable and computed are not working properly Knockout

  13. 13

    Knockout computed observable with access to observableArray

  14. 14

    Inheritance and overriding Knockout computed observable

  15. 15

    Inheritance and overriding Knockout computed observable

  16. 16

    knockout computed array not updating when function updates observable array used in computed

  17. 17

    Knockout/JS Referencing Observable In Other Function

  18. 18

    Cant get data to observable in knockout js

  19. 19

    Knockout computed not updating using observable array

  20. 20

    Knockout : find out which observable triggerred computed

  21. 21

    Knockout chained computed observable throws exception on update

  22. 22

    Knockout computed creates dependency for observable that is written to but not read

  23. 23

    Knockout Js computed not working in a model

  24. 24

    Knockout js Computed not being fired

  25. 25

    Knockout js Computed not being fired

  26. 26

    Practical Example using computed observable with another context

  27. 27

    Second computed function causes error in Knockout code

  28. 28

    Asynchronous call inside a Knockout computed function

  29. 29

    Knockout js computed running balance infinite loops

HotTag

Archive