2

http://jsfiddle.net/hRksW/

function test() {
    this.alerting = function () {
        alert("test");
    };
    this.something = function () {
        setInterval(function () {
            this.alerting();
        }, 1000);
    };

}

var a = new test();
a.something();

Calling the function something() should call the function alerting() every second. This should alert 'test' every second. Why doesn't that happen and how can I make it happen? Note that I want to keep this design of calling a method in a method, if possible.

George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • 2
    Relevant: [JavaScript “this” keyword](http://stackoverflow.com/q/3127429/251311) – zerkms Jul 14 '14 at 10:04
  • It does not happen because `this` is not a variable. It's a keyword that refers to the owner of current context. Inside an anonymous function it will be the global `window` (or `undefined` if in strict mode). – freakish Jul 14 '14 at 10:24
  • @zerkms that link was really helpful – George Irimiciuc Jul 14 '14 at 13:41

6 Answers6

2

http://jsfiddle.net/N6hPB/

function test() {
    this.alerting = function () {
        alert("test");
    };
    this.something = function () {
        setInterval(this.alerting, 1000);
    };    
}

var a = new test();
a.something();
zavg
  • 10,351
  • 4
  • 44
  • 67
2

Store a reference of this in a variable and use it for method that run outside of the current context (like the setInterval does)

function test() {
    var that = this;

    this.alerting = function () {
        alert("test");
    };
    this.something = function () {
        setInterval(function () {
            that.alerting();
        }, 1000);
    };

}

var a = new test();
a.something();
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • This seems to work, but why do I have to store 'this' in a variable and why isn't my code working? – George Irimiciuc Jul 14 '14 at 10:26
  • 1
    @GeorgeIrimiciuc, `this` inside setInterval's function parameter belongs to inner function context, not outer context, `this` always refers to current context – Yuliam Chandra Jul 14 '14 at 10:29
  • @GeorgeIrimiciuc, what Yuliam said. `this` is a special keyword and its content is specified by the context the function is running. The `setTimeout` context is the `window` so `this` inside methods triggered by `setTimout` point to the `window`. – Gabriele Petrioli Jul 14 '14 at 11:00
0

Hope this helps! Here the timer is in ms.

function something(){
   window.setInterval(alerting, 1000); 
}

function alerting() { 
  alert('test'); 
}
Aravind
  • 609
  • 6
  • 14
0

Another way to do it is returning an object instead.

function test() {
    var self = {
        alerting : function () {
            console.log("test");
        },
        something : function () {
            setInterval(function () {
                self.alerting();
            }, 1000);
        }
    };
    return self;
}

var a = new test();
a.something();
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
0

You can do it making an object with functions, and then call them without instantiating it.

var testCaller = {
    alerting: function() {
       alert("test")
    },
    something: function() {
       // Store current scope
       var self = this;
       setInterval(function() {
           self.alerting();
       }, 1000);
    }
}

testCaller.something();
wIRELESS
  • 196
  • 3
  • 3
0

You can try this code. I have tested it, and it works. In your code, "this" pointer points to somewhere else.

function test() {

    test.prototype.alerting = function() {
        alert("alert test");
    };

    test.prototype.something = function () {
        setInterval(this.alerting, 1000);
    };
}

var a = new test();
a.something();