Node.js vm.isContext() Method
Last Updated :
02 Feb, 2022
The vm.isContext() method is an inbuilt application programming interface of the vm module which is used to check if the stated object is being contextified using vm.createContext() method or not.
Syntax:
vm.isContext( object )
Parameters: This method accepts single parameter object.
Return Value: It returns true if the stated object is being contextified else it returns false.
Below examples illustrate the use of vm.isContext() method in Node.js:
Example 1:
javascript
const util = require( 'util' );
const vm = require( 'vm' );
global.globalVar = 7;
const object = { globalVar: 3 };
vm.createContext(object);
vm.runInContext( 'globalVar *= 6;' , object);
console.log(object);
console.log(global.globalVar);
vm.isContext(object);
|
Output:
{ globalVar: 18 }
7
true
Here, globalVar in the context is 18 in output as (6*3 = 18) but the value of globalVar is still 7. Moreover, here the stated object is being contextified so true is returned.
Example 2:
javascript
const util = require( 'util' );
const vm = require( 'vm' );
global.globalVar = 7;
const object = { globalVar: 3 };
console.log(object);
console.log(global.globalVar);
vm.isContext(object);
|
Output:
{ globalVar: 3 }
7
false
Here, the stated object is not contextified so, false is returned.
Reference: https://nodejs.org/api/vm.html#vm_vm_iscontext_object
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...