From 22f3813b3e1a2668d8623d04bdd9178007bb3d51 Mon Sep 17 00:00:00 2001 From: AnnaMag Date: Thu, 29 Dec 2016 13:00:43 +0000 Subject: [PATCH] doc: clarify the statement in vm.createContext() PR-URL: https://github.com/nodejs/node/pull/10519 Reviewed-By: James M Snell Reviewed-By: Prince John Wesley Reviewed-By: Franziska Hinkelmann --- doc/api/vm.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/api/vm.md b/doc/api/vm.md index a62923c2a1a317..8c49cfb7c31f24 100644 --- a/doc/api/vm.md +++ b/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.