Node.js VM 모듈
예시
"가상 머신"에서 일부 JavaScript 코드 실행:
var vm = require('vm');
var myObj = { name: 'John', age: 38 };
vm.createContext(myObj);
vm.runInContext('age += 1;', myObj);
console.log(myObj);
정의 및 사용
VM 모듈은 JavaScript의 eval()과 거의 유사하게 가상 머신에서 JavaScript를 실행하는 방법을 제공합니다.
통사론
애플리케이션에 VM 모듈을 포함하기 위한 구문:
var vm = require('vm');
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 |