Skip to content

Commit dcc20f1

Browse files
AnnaMagevanlucas
authored andcommitted
doc: clarify the statement in vm.createContext()
PR-URL: #10519 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Prince John Wesley <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 8e78953 commit dcc20f1

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

doc/api/vm.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,24 @@ that sandbox][contextified] so that it can be used in calls to
212212
[`vm.runInContext()`][] or [`script.runInContext()`][]. Inside such scripts,
213213
the `sandbox` object will be the global object, retaining all of its existing
214214
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+
```
217233

218234
If `sandbox` is omitted (or passed explicitly as `undefined`), a new, empty
219235
[contextified][] sandbox object will be returned.

0 commit comments

Comments
 (0)