Skip to content

Commit 8f25e51

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 464f4af commit 8f25e51

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

doc/api/vm.md

+16-21
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])`
@@ -804,9 +802,11 @@ vm.createContext(context);
804802
805803
vm.runInContext('globalVar *= 2;', context);
806804
807-
console.log(util.inspect(context)); // { globalVar: 2 }
805+
console.log(context);
806+
// Prints: { globalVar: 2 }
808807
809-
console.log(util.inspect(globalVar)); // 3
808+
console.log(global.globalVar);
809+
// Prints: 3
810810
```
811811

812812
If `contextObject` is omitted (or passed explicitly as `undefined`), a new,
@@ -906,9 +906,8 @@ vm.createContext(contextObject);
906906
for (let i = 0; i < 10; ++i) {
907907
vm.runInContext('globalVar *= 2;', contextObject);
908908
}
909-
console.log(util.inspect(contextObject));
910-
911-
// { globalVar: 1024 }
909+
console.log(contextObject);
910+
// Prints: { globalVar: 1024 }
912911
```
913912
914913
## `vm.runInNewContext(code[, contextObject[, options]])`
@@ -1003,9 +1002,8 @@ const contextObject = {
10031002
};
10041003
10051004
vm.runInNewContext('count += 1; name = "kitty"', contextObject);
1006-
console.log(util.inspect(contextObject));
1007-
1008-
// { animal: 'cat', count: 3, name: 'kitty' }
1005+
console.log(contextObject);
1006+
// Prints: { animal: 'cat', count: 3, name: 'kitty' }
10091007
```
10101008
10111009
## `vm.runInThisContext(code[, options])`
@@ -1075,15 +1073,12 @@ const vm = require('vm');
10751073
let localVar = 'initial value';
10761074
10771075
const vmResult = vm.runInThisContext('localVar = "vm";');
1078-
console.log('vmResult:', vmResult);
1079-
console.log('localVar:', localVar);
1076+
console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
1077+
// Prints: vmResult: 'vm', localVar: 'initial value'
10801078
10811079
const evalResult = eval('localVar = "eval";');
1082-
console.log('evalResult:', evalResult);
1083-
console.log('localVar:', localVar);
1084-
1085-
// vmResult: 'vm', localVar: 'initial value'
1086-
// evalResult: 'eval', localVar: 'eval'
1080+
console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
1081+
// Prints: evalResult: 'eval', localVar: 'eval'
10871082
```
10881083
10891084
Because `vm.runInThisContext()` does not have access to the local scope,

0 commit comments

Comments
 (0)