views:

49

answers:

1

Dear Masterminds,

I'm currently wondering if there is a better solution than passing this scope to the lambda-function via the parameter 'e' and then passing it to 'funkyFunction' using call()-method

setInterval(function(e){e.funkyFunction.call(e)}, speed, this)

(Minor question aside: I'd been reading something about memory-leaks in javascript. How does the lambda-function affect my memory? Is it better to define it first like var i = function(e)... and then passing it as a parameter to setInterval?)

+1  A: 

What's wrong with simply relying on the outer-scope defined variable?

(function() { 

    var x = {};
    setInterval(function() {
       funkyFunction.call(x)
    }, speed);

})();
meder
My Problem is, that setInterval is called out of another method of (in your example) 'x'. So I'm basically calling one method out of the other and tried find a tricky solution around closures, conserving a hugh object. Twisted thoughts?
PenthousePauper
@PenthousePauper: Having a reference to the object in a closure won't cause any memory issues. Passing `this` to `setInterval` would keep the object alive anyway. It's also Firefox-specific, so it won't work everywhere.
Matthew Crumley