Skip to content

Instantly share code, notes, and snippets.

@clochix
Created October 10, 2012 08:27
Show Gist options
  • Save clochix/3864034 to your computer and use it in GitHub Desktop.
Save clochix/3864034 to your computer and use it in GitHub Desktop.
JavaScript Sandbox
// Clone an object
// @see https://developer.mozilla.org/en-US/docs/Differential_inheritance_in_JavaScript
Object.prototype.clone = function(){
// Extract the prototype from the "this" object
var p = Object.getPrototypeOf(this);
// Create a new object with p as its prototype
return Object.create(p);
};
var myGlobalObject = {},
globalClone = myGlobalObject.clone();
myGlobalObject.myProp = "initial value";
(function(myGlobalObject){
// sandboxed code
myGlobalObject.myProp = "new value";
console.log(myGlobalObject.myProp); // ⇒ "new value"
}(globalClone));
console.log(myGlobalObject.myProp); // ⇒ "initial value"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment