vm.isContext()方法是vm模塊的內置應用程序編程接口,用於檢查指定的對象是否正在使用vm.createContext()方法進行上下文化。
用法:
vm.isContext( object )
參數:此方法接受單個參數對象。
返回值:如果聲明的對象正在上下文中,則返回true,否則返回false。
以下示例說明了Node.js中vm.isContext()方法的使用:
範例1:
// Node.js program to demonstrate the
// vm.isContext() method
// Including util and vm module
const util = require('util');
const vm = require('vm');
// Assigning value to the global variable
global.globalVar = 7;
// Defining Context object
const object = { globalVar:3 };
// Contextifying stated object
// using createContext method
vm.createContext(object);
// Compiling code
vm.runInContext('globalVar *= 6;', object);
// Displays the context
console.log(object);
// Dsiplays value of global variable
console.log(global.globalVar);
// Calling isContext method
vm.isContext(object);
輸出:
{ globalVar:18 } 7 true
在這裏,上下文中的globalVar在輸出中為18(6 * 3 = 18),但是globalVar的值仍為7。此外,此處聲明的對象處於上下文中,因此返回true。
範例2:
// Node.js program to demonstrate the
// vm.isContext() method
// Including util and vm module
const util = require('util');
const vm = require('vm');
// Assigning value to the global variable
global.globalVar = 7;
// Defining Context object
const object = { globalVar:3 };
// Displays the context
console.log(object);
// Dsiplays value of global variable
console.log(global.globalVar);
// Calling isContext method
vm.isContext(object);
輸出:
{ globalVar:3 } 7 false
在此,聲明的對象沒有上下文,因此返回false。
參考: https://nodejs.org/api/vm.html#vm_vm_iscontext_object
相關用法
- Node.js GM resize()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM quality()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM whiteThreshold()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | vm.isContext() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。