Created
May 16, 2012 06:26
-
-
Save josher19/2708039 to your computer and use it in GitHub Desktop.
Simple shallow copy of global will not work as expected with runInNewContext
This file contains hidden or 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
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