Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that, unlike in a language like python, when you take a dafunc.play and pass it somewhere else (callback = dafunc.play) 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.</p> <pre><code>var ttimer = new myns.Timer(1000, function(){ return dafunc.play(); }); </code></pre> <hr> <p>Making all there extra functions by yourself is annoying. You could instead use the bind method that is available in newer browsers:</p> <pre><code>var wrappedDing = this.ding.bind(this); new myns.Timer(1000, dafunc.play.bind(dafunc) ); </code></pre> <p>Or you could use a similar shim if you need to support older versions of IE too.</p> <hr> <p>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 <code>this</code> anymore:</p> <p>(btw, I ended up simplifying the code in the proccess...)</p> <pre><code>myns = {}; myns.somefunc = function(txt) { var obj = { data : txt }; obj.play = function() { alert(obj.data); }; return obj; }; var dafunc = myns.somefunc('hello world'); myns.timer = function(msec, callback) { var timerID = null; var set = function(){ stop(); timerID = setTimeout(callback, msec); }; set(); return { set: set }; }; var ttimer = myns.timer(1000, dafunc.play); </code></pre> <hr> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload