Skip to content

Instantly share code, notes, and snippets.

@rwaldron

rwaldron/use.js Secret

Created July 25, 2014 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/7a3fe8bd49831978846e to your computer and use it in GitHub Desktop.
Save rwaldron/7a3fe8bd49831978846e to your computer and use it in GitHub Desktop.
require("es6-shim");
var lib = (function () {
function grant(target, source) {
Object.assign(target, source);
}
function revoke(target, source) {
Object.keys(source).forEach(function(key) {
delete target[key];
});
}
var hasOwn = Object.prototype.hasOwnProperty;
var shared = {
message: "the message"
};
return {
invoker: function(foreign) {
for (var p in foreign) {
if (hasOwn.call(foreign, p) && typeof foreign[p] === "function") {
// grant access
grant(foreign[p], shared);
// invoke
foreign[p]();
// revoke access
revoke(foreign[p], shared);
}
}
}
};
}());
var foreign = {
a: function() {
console.log("Within a:", this.a.message);
}
};
Object.defineProperty(foreign, "b", {
value: function() {
console.log("Within b:", this.b.message);
},
enumerable: true,
writable: false,
configurable: false
});
lib.invoker(foreign);
// "Within a: the message"
// "Within b: the message"
console.log( foreign.a.message === undefined );
console.log( foreign.b.message === undefined );
// true
// true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment