Skip to content

Comment on Closures for class/module privacy considered harmful

Comments

Can't you expose the internal methods individually via the module for testing purposes without making them vulnerable to manipulation as they are used by the public methods?

Just tried this in the developer console:

    // module 'm' exposes 'p' that uses 'f' internally. Exposes
    // 'f' for testing purposes.
    var m = (function() { var f = function() { return 'a'; }; var p = function() { return f(); }; return {'p': p, 'internalF': f};})();

    m.p();
    // 'a'

    m.internalF();
    // 'a'

    m.internalF = function() { return 'haxor';};
    m.internalF();
    // 'haxor'

    m.p();
    // 'a'
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.