| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1f44922 commit 22f3813
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,8 +212,24 @@ that sandbox][contextified] so that it can be used in calls to | |||
| 212 | 212 | [`vm.runInContext()`][] or [`script.runInContext()`][]. Inside such scripts, | |
| 213 | 213 | the `sandbox` object will be the global object, retaining all of its existing | |
| 214 | 214 | properties but also having the built-in objects and functions any standard | |
| 215 | - [global object][] has. Outside of scripts run by the vm module, `sandbox` will | ||
| 216 | - remain unchanged. | ||
| 215 | + [global object][] has. Outside of scripts run by the vm module, global variables | ||
| 216 | + will remain unchanged. | ||
| 217 | + | ||
| 218 | + ```js | ||
| 219 | + const util = require('util'); | ||
| 220 | + const vm = require('vm'); | ||
| 221 | + | ||
| 222 | + var globalVar = 3; | ||
| 223 | + | ||
| 224 | + const sandbox = { globalVar: 1 }; | ||
| 225 | + vm.createContext(sandbox); | ||
| 226 | + | ||
| 227 | + vm.runInContext('globalVar *= 2;', sandbox); | ||
| 228 | + | ||
| 229 | + console.log(util.inspect(sandbox)); // 2 | ||
| 230 | + | ||
| 231 | + console.log(util.inspect(globalVar)); // 3 | ||
| 232 | + ``` | ||
| 217 | 233 | ||
| 218 | 234 | If `sandbox` is omitted (or passed explicitly as `undefined`), a new, empty | |
| 219 | 235 | [contextified][] sandbox object will be returned. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments