Browse Source

doc: clarify the statement in vm.createContext()

PR-URL: https://github.com/nodejs/node/pull/10519
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
v6
AnnaMag 8 years ago
committed by Franziska Hinkelmann
parent
commit
c57caf5c69
  1. 20
      doc/api/vm.md

20
doc/api/vm.md

@ -212,8 +212,24 @@ that sandbox][contextified] so that it can be used in calls to
[`vm.runInContext()`][] or [`script.runInContext()`][]. Inside such scripts,
the `sandbox` object will be the global object, retaining all of its existing
properties but also having the built-in objects and functions any standard
[global object][] has. Outside of scripts run by the vm module, `sandbox` will
remain unchanged.
[global object][] has. Outside of scripts run by the vm module, global variables
will remain unchanged.
```js
const util = require('util');
const vm = require('vm');
var globalVar = 3;
const sandbox = { globalVar: 1 };
vm.createContext(sandbox);
vm.runInContext('globalVar *= 2;', sandbox);
console.log(util.inspect(sandbox)); // 2
console.log(util.inspect(globalVar)); // 3
```
If `sandbox` is omitted (or passed explicitly as `undefined`), a new, empty
[contextified][] sandbox object will be returned.

Loading…
Cancel
Save