Node.js | vm.isContext() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.