Node.js VM मॉड्यूल
उदाहरण
"वर्चुअल मशीन" में कुछ जावास्क्रिप्ट कोड चलाएँ:
var vm = require('vm');
var myObj = { name: 'John', age: 38 };
vm.createContext(myObj);
vm.runInContext('age += 1;', myObj);
console.log(myObj);
परिभाषा और उपयोग
वीएम मॉड्यूल वर्चुअल मशीन पर जावास्क्रिप्ट को निष्पादित करने का एक तरीका प्रदान करता है, लगभग जावास्क्रिप्ट में eval () की तरह।
वाक्य - विन्यास
आपके आवेदन में VM मॉड्यूल को शामिल करने का सिंटैक्स:
var vm = require('vm');
वीएम गुण और तरीके
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 |