In javascript need help wrapping a function and handling “this” properly

I started trying to create a timer function that would let me wrap a callback function so that I could later alter the behavior dynamically.

This led to a general realization that I really don't understand functions yet, and definitely don't understand what is happening with 'this'

I have a test environment setup on jsfiddle

myns = {};
myns.somefunc = function(txt) { = txt; = function() {
        alert( + ' : ';

var dafunc = new myns.somefunc('hello world');

myns.Timer = function(msec, callback) {
    this.callback = null;
    this.timerID = null;

    this.ding = function() {

    this.set1 = function( msec, callback ) {
        this.callback = callback;
        this.timerID = setTimeout(this.ding, msec );

    this.set2 = function( msec, callback ) {
        this.callback = callback;
        var wrappedDing = (function(who) {
            return function() {
        this.timerID = setTimeout(wrappedDing, msec );
    //this.set1(msec, callback);
    this.set2(msec, callback);    

var ttimer = new myns.Timer(1000,;

If I use the set1 method, then the callback doesn't work. So I am trying the set2 method. This gets me to the play method but "this" is not referring to the instance of somefunc.

I thought I was on the right track, but the mix up on 'this' has me confused.

Any clues would be welcome.


The problem is that, unlike in a language like python, when you take a and pass it somewhere else (callback = it forgets it was associated with dafunc, son you you would need to use yet another wrapper function, like you did in the set2 function.

var ttimer = new myns.Timer(1000, function(){ return; });

Making all there extra functions by yourself is annoying. You could instead use the bind method that is available in newer browsers:

var wrappedDing = this.ding.bind(this);

new myns.Timer(1000, );

Or you could use a similar shim if you need to support older versions of IE too.

Finally, if you are not going to take advantage of some form of inheritance or dynamic binding, you could instead rewrite your code to use closures. Since everything is lexicaly scoped, you don't have to worry about the this anymore:

(btw, I ended up simplifying the code in the proccess...)

myns = {};

myns.somefunc = function(txt) {
    var obj = { data : txt }; = function() {
    return obj;

var dafunc = myns.somefunc('hello world');

myns.timer = function(msec, callback) {
    var timerID = null;

    var set = function(){
        timerID = setTimeout(callback, msec);


    return { 
         set: set

var ttimer = myns.timer(1000,;

And one last thing: If you don't hate yourself use console.log and your browser's debugger and development console instead of using alerts for output.

Need Your Help

adding value limitations on list comprehension

python colors rgb list-comprehension

I am trying to find a clean way of grabbing the difference between the values of two int tuples. The tuples themselves have limitations though as they represent RGB colors. This is what I have so f...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.