mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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>
This commit is contained in:
1 parent
8e78953c88
commit
dcc20f12a6
1 file changed
+18
-2
+18
-2
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user