Skip to content

Instantly share code, notes, and snippets.

@josher19
Created May 16, 2012 06:26
Show Gist options
  • Save josher19/2708039 to your computer and use it in GitHub Desktop.
Save josher19/2708039 to your computer and use it in GitHub Desktop.
Simple shallow copy of global will not work as expected with runInNewContext
function clone(obj) { var nobj={}, key; for(key in obj) nobj[key]=obj[key]; return nobj; }
var local = null
var g = clone(global);
local = 3
console.assert(local === 3, 'not initialized to 3')
console.assert(null == g.local, 'g.local should be null or undefined')
vm.runInNewContext("console.log(local=4)", g)
console.assert(local === 3, 'local unchanged')
console.assert(4 == g.local, 'g.local should be 4')
vm.runInThisContext("console.log(local=5)")
console.assert(local === 5, 'local should be changed to 5 by vm.runInThisContext')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment