4

I'm trying to understand below code :

var MyModel = function (folders) {
    var thisType = this;

    this.callType = ko.computed({
        read: function () {
            return "openLink";
        },
        owner: this
    });

Why set 'thisType' to 'this' ?

Reading the doc at http://knockoutjs.com/documentation/computedObservables.html

"Managing ‘this’ In case you’re wondering what the second parameter to ko.computed is (the bit where we passed this in the preceding code), that defines the value of this when evaluating the computed observable. Without passing it in, it would not have been possible to refer to this.firstName() or this.lastName(). Experienced JavaScript coders will regard this as obvious, but if you’re still getting to know JavaScript it might seem strange. (Languages like C# and Java never expect the programmer to set a value for this, but JavaScript does, because its functions themselves aren’t part of any object by default.)"

Is 'this' the page object ?

Mark Schultheiss
  • 28,892
  • 9
  • 63
  • 88
blue-sky
  • 45,835
  • 124
  • 360
  • 647
  • 1
    Probably the variable is used somewhere below… show us the whole code! If not, you can omit it. – Bergi May 13 '13 at 11:50
  • [`this`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this) is a special keyword that refers to the "context", and depends on how the function is called. In `new MyModel()`, it will be the instance for example. – Bergi May 13 '13 at 11:52
  • possible duplicate of [JavaScript "this" keyword](http://stackoverflow.com/questions/3127429/javascript-this-keyword) – Mark Schultheiss May 13 '13 at 11:52
  • this refers back to the calling context of the function. Please do a search here on SO before posting. Here is a very relevant thread JavaScript "this" keyword – H.Rabiee May 13 '13 at 11:55

2 Answers2

3

in JS this refers to the context execution

I like this excerpt from quirksmode.org:

In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of. When we define our faithful function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript. An onclick property, though, is owned by the HTML element it belongs to.

So that in your example this will have the value of the context of MyModel at moment instantiation.

Jeromy French
  • 11,372
  • 13
  • 67
  • 119
Agus
  • 1,516
  • 2
  • 21
  • 46
3

I'm glad you asked this question. I like to think of myself a fairly experienced js programmer, but I still did a double take when I came across this in the knockout js docs.

Why set thistype to this?

When calling an external function you may still want access to the object that this refers to. Utilising a closure this is possible by saving this to a variable in the parent function.

The best example I can think of is an onclick event.

<div id="test">content</div>

document.getElementById("test").onclick = function(e){
    var self = this;

    function test(){
        alert(this); //[object Window]
        alert(self); //[object HTMLDivElement]
    }
    test();
};

Is 'this' the page object ?

As other people have pointed out, there is a lot of documentation about the this keyword.

gkiely
  • 2,905
  • 1
  • 21
  • 35