How to break closures in JavaScript

Posted by Not a Name on Stack Overflow See other posts from Stack Overflow or by Not a Name
Published on 2011-01-09T20:44:58Z Indexed on 2011/01/09 20:53 UTC
Read the original article Hit count: 137

Filed under:

Is there any way to break a closure easily in JavaScript? The closest I have gotten is this:

var src = 3;
function foo () {
    return function () {
        return src; }
    }
function bar (func) {
    var src = 9;
    return eval('('+func.toString()+')')();  // Line breaks closure
}
alert(bar(foo()));

Which prints 9, instead of 3 as a closure would dictate. However, this approach seems kind of ugly to me, are there any better ways?

© Stack Overflow or respective owner

Related posts about JavaScript