Created
April 11, 2010 20:57
-
-
Save DmitrySoshnikov/363056 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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