Node.js- VM - Modul

❮ Eingebaute Module


Beispiel

Führen Sie JavaScript-Code in einer "virtuellen Maschine" aus:

var vm = require('vm');
var myObj = { name: 'John', age: 38 };
vm.createContext(myObj);

vm.runInContext('age += 1;', myObj);

console.log(myObj);

Definition und Verwendung

Das VM-Modul bietet eine Möglichkeit, JavaScript auf einer virtuellen Maschine auszuführen, fast wie eval() in JavaScript.


Syntax

Die Syntax zum Einbinden des VM-Moduls in Ihre Anwendung:

var vm = require('vm');

VM-Eigenschaften und -Methoden

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

❮ Eingebaute Module