Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Owner of "this"

I followed a tutorial for creating a JavaScript stopwatch and am trying to expand it to work with multiple stopwatches (multiple instances of a class). The problem I have is when I am trying to display the current value while the clock is ticking I need to hard code the class instance becasue using "this" doesn't work (on the line where I am using console.log). I have cut the code down to the minimum to try to understand this aspect, and have pasted what I have below:

function Timer(){
    var time1 = null;
    var time2 = null;
    var timeLoop = null;

    function getTime(){
        var day = new Date();
        return day.getTime();
    }

    this.start = function(){
        time1 = getTime();

        timeLoop = setInterval(function(){
            time2 = getTime();
            console.log(_Timer.duration());
            //console.log(this.duration());
        },500);
    }

    this.duration = function(){
        return (time1 - time2) / 1000;
    }

}       

I think the link below describes my problem but I don't understand it enough to apply it here. Is the issue due to the owner being this.start rather than just this and how can I ammend the code to make it work with any instance of Timer?

http://www.quirksmode.org/js/this.html

I have included both the hard coded value line and the "this" line that doesn't work.

Thanks,

Geraint

like image 420
Geraint Anderson Avatar asked Jul 02 '14 19:07

Geraint Anderson


People also ask

Who is owner of function in JavaScript?

The function belongs to the object. myObject is the owner of the function. The thing called this , is the object that "owns" the JavaScript code. In this case the value of this is myObject.

What is this JavaScript?

What is this? In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

Why do we use this in JavaScript?

In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.

What is this in a function?

The this keyword refers to the object the function belongs to, or the window object if the function belongs to no object. It's used in OOP code, to refer to the class/object the function belongs to For example: function foo() { this.


2 Answers

If you want to have the this property be consistent, you should bind the functions that are being called.

For example,

setInterval(function() { /* code here */ }.bind(this), 500)

That way, the this of the inner function will be the same as that of the outer function.

like image 104
Strikeskids Avatar answered Nov 10 '22 01:11

Strikeskids


Whenever you see function you can assume the value of this changes, so inside the callback function for the interval this is actually the window, not the object.

The easy solution is to just store this in a variable

function Timer(){
    var time1 = null;
    var time2 = null;
    var timeLoop = null;

    function getTime(){
        var day = new Date();
        return day.getTime();
    }

    this.start = function(){

        var self = this;

        time1 = getTime();

        timeLoop = setInterval(function(){
            time2 = getTime();
            console.log(self.duration());
        },500);
    }

    this.duration = function(){
        return (time1 - time2) / 1000;
    }

}    
like image 42
adeneo Avatar answered Nov 10 '22 01:11

adeneo