Módulo de máquina virtual de Node.js
Ejemplo
Ejecute algún código JavaScript en una "máquina virtual":
var vm = require('vm');
var myObj = { name: 'John', age: 38 };
vm.createContext(myObj);
vm.runInContext('age += 1;', myObj);
console.log(myObj);
Definición y uso
El módulo VM proporciona una forma de ejecutar JavaScript en una máquina virtual, casi como eval() en JavaScript.
Sintaxis
La sintaxis para incluir el módulo VM en su aplicación:
var vm = require('vm');
Propiedades y métodos de la máquina virtual
Method | Description |
---|---|
createContext() | Prepares a virtual machine, or sandbox, where you can execute scripts |
isContext() | Returns true if the specified sandbox has been created by the createContext() method |
runInContext() | Executes JavaScript code in the specified context, and returns the result |
runInDebug() | Executes JavaScript inside the debug context |
runInNewContext() | Executes JavaScript code in a new context, and returns the result |
runInThisContext() | Executes JavaScript code in the global context, and returns the result |