Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DmitrySoshnikov/363056 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/363056 to your computer and use it in GitHub Desktop.
/* by Dmitry A. Soshnikov */
var object = (function () {
// private
var _x = 10;
// also private
function _privateFunc() {
return _x;
}
// public
function publicFunc() {
return 10 + _privateFunc();
}
// exports return only public properties/methods
return exports(/*AO*/function () {}.__parent__);
})();
function exports(AO) {
var
hasOwn = Object.prototype.hasOwnProperty,
api = {};
for (var property in AO) if (hasOwn.call(AO, property)) {
if (property.charAt(0) == '_') {
continue;
}
api[property] = AO[property];
}
return api;
}
print("publicFunc" in object); // true
print("_privateFunc" in object); // false
// arguments also has been exported
// but can be filtered
print("arguments" in object); // true
print(object.publicFunc()); // 20
print(object._privateFunc()); // Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment