Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: update vm module examples #1147

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/api/vm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ code does not have access to local scope, but does have access to the current

Example of using `vm.runInThisContext` and `eval` to run the same code:

var vm = require('vm');
var localVar = 'initial value';

var vmResult = vm.runInThisContext('localVar = "vm";');
Expand Down Expand Up @@ -117,7 +118,7 @@ Example: compile and execute code that increments a global variable and sets a
new one. These globals are contained in the sandbox.

var util = require('util');
var vm = require('vm'),
var vm = require('vm');

var sandbox = {
animal: 'cat',
Expand Down Expand Up @@ -223,10 +224,11 @@ execute the code multiple times. These globals are contained in the sandbox.
count: 2
};

var context = new vm.createContext(sandbox);
var script = new vm.Script('count += 1; name = "kitty"');

for (var i = 0; i < 10; ++i) {
script.runInContext(sandbox);
script.runInContext(context);
}

console.log(util.inspect(sandbox));
Expand Down