From 8c78b87d97e0ac5dfdec49577fadb5fc9cd6bb47 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Thu, 23 Jan 2020 16:02:40 +0200 Subject: [PATCH] doc: clean up and streamline vm.md examples PR-URL: https://github.com/nodejs/node/pull/31474 Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott --- doc/api/vm.md | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/doc/api/vm.md b/doc/api/vm.md index 408808db37370b..e6792385ef2138 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -175,9 +175,8 @@ for (let i = 0; i < 10; ++i) { script.runInContext(context); } -console.log(util.inspect(context)); - -// { animal: 'cat', count: 12, name: 'kitty' } +console.log(context); +// Prints: { animal: 'cat', count: 12, name: 'kitty' } ``` Using the `timeout` or `breakOnSigint` options will result in new event loops @@ -246,9 +245,8 @@ contexts.forEach((context) => { script.runInNewContext(context); }); -console.log(util.inspect(contexts)); - -// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }] +console.log(contexts); +// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }] ``` ### `script.runInThisContext([options])` @@ -778,9 +776,11 @@ vm.createContext(context); vm.runInContext('globalVar *= 2;', context); -console.log(util.inspect(context)); // { globalVar: 2 } +console.log(context); +// Prints: { globalVar: 2 } -console.log(util.inspect(globalVar)); // 3 +console.log(global.globalVar); +// Prints: 3 ``` If `contextObject` is omitted (or passed explicitly as `undefined`), a new, @@ -880,9 +880,8 @@ vm.createContext(contextObject); for (let i = 0; i < 10; ++i) { vm.runInContext('globalVar *= 2;', contextObject); } -console.log(util.inspect(contextObject)); - -// { globalVar: 1024 } +console.log(contextObject); +// Prints: { globalVar: 1024 } ``` ## `vm.runInNewContext(code[, contextObject[, options]])` @@ -977,9 +976,8 @@ const contextObject = { }; vm.runInNewContext('count += 1; name = "kitty"', contextObject); -console.log(util.inspect(contextObject)); - -// { animal: 'cat', count: 3, name: 'kitty' } +console.log(contextObject); +// Prints: { animal: 'cat', count: 3, name: 'kitty' } ``` ## `vm.runInThisContext(code[, options])` @@ -1049,15 +1047,12 @@ const vm = require('vm'); let localVar = 'initial value'; const vmResult = vm.runInThisContext('localVar = "vm";'); -console.log('vmResult:', vmResult); -console.log('localVar:', localVar); +console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`); +// Prints: vmResult: 'vm', localVar: 'initial value' const evalResult = eval('localVar = "eval";'); -console.log('evalResult:', evalResult); -console.log('localVar:', localVar); - -// vmResult: 'vm', localVar: 'initial value' -// evalResult: 'eval', localVar: 'eval' +console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`); +// Prints: evalResult: 'eval', localVar: 'eval' ``` Because `vm.runInThisContext()` does not have access to the local scope,