-
-
Save rwaldron/7a3fe8bd49831978846e 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
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