Skip to content

Commit 8c78b87

Browse files
lundibundicodebytere
authored andcommitted
doc: clean up and streamline vm.md examples
PR-URL: #31474 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 604ce0a commit 8c78b87

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

doc/api/vm.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ for (let i = 0; i < 10; ++i) {
175175
script.runInContext(context);
176176
}
177177

178-
console.log(util.inspect(context));
179-
180-
// { animal: 'cat', count: 12, name: 'kitty' }
178+
console.log(context);
179+
// Prints: { animal: 'cat', count: 12, name: 'kitty' }
181180
```
182181

183182
Using the `timeout` or `breakOnSigint` options will result in new event loops
@@ -246,9 +245,8 @@ contexts.forEach((context) => {
246245
script.runInNewContext(context);
247246
});
248247

249-
console.log(util.inspect(contexts));
250-
251-
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
248+
console.log(contexts);
249+
// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
252250
```
253251

254252
### `script.runInThisContext([options])`
@@ -778,9 +776,11 @@ vm.createContext(context);
778776

779777
vm.runInContext('globalVar *= 2;', context);
780778

781-
console.log(util.inspect(context)); // { globalVar: 2 }
779+
console.log(context);
780+
// Prints: { globalVar: 2 }
782781

783-
console.log(util.inspect(globalVar)); // 3
782+
console.log(global.globalVar);
783+
// Prints: 3
784784
```
785785
786786
If `contextObject` is omitted (or passed explicitly as `undefined`), a new,
@@ -880,9 +880,8 @@ vm.createContext(contextObject);
880880
for (let i = 0; i < 10; ++i) {
881881
vm.runInContext('globalVar *= 2;', contextObject);
882882
}
883-
console.log(util.inspect(contextObject));
884-
885-
// { globalVar: 1024 }
883+
console.log(contextObject);
884+
// Prints: { globalVar: 1024 }
886885
```
887886
888887
## `vm.runInNewContext(code[, contextObject[, options]])`
@@ -977,9 +976,8 @@ const contextObject = {
977976
};
978977

979978
vm.runInNewContext('count += 1; name = "kitty"', contextObject);
980-
console.log(util.inspect(contextObject));
981-
982-
// { animal: 'cat', count: 3, name: 'kitty' }
979+
console.log(contextObject);
980+
// Prints: { animal: 'cat', count: 3, name: 'kitty' }
983981
```
984982
985983
## `vm.runInThisContext(code[, options])`
@@ -1049,15 +1047,12 @@ const vm = require('vm');
10491047
let localVar = 'initial value';
10501048

10511049
const vmResult = vm.runInThisContext('localVar = "vm";');
1052-
console.log('vmResult:', vmResult);
1053-
console.log('localVar:', localVar);
1050+
console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
1051+
// Prints: vmResult: 'vm', localVar: 'initial value'
10541052

10551053
const evalResult = eval('localVar = "eval";');
1056-
console.log('evalResult:', evalResult);
1057-
console.log('localVar:', localVar);
1058-
1059-
// vmResult: 'vm', localVar: 'initial value'
1060-
// evalResult: 'eval', localVar: 'eval'
1054+
console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
1055+
// Prints: evalResult: 'eval', localVar: 'eval'
10611056
```
10621057
10631058
Because `vm.runInThisContext()` does not have access to the local scope,

0 commit comments

Comments
 (0)