Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Lodash _.runInContext() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.runInContext() method is used to create a new lodash function using the given context object.

Syntax:

_.runInContext( context )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • context: It holds the context object with which the new function has to be created.

Return Value: This method returns a new lodash function.

Example 1:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
  
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
   
var func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });    
  
// Using the value() method
let val = _.isFunction(_.foo);
  
// Display the output 
console.log(val);

Output:

true

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
  
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
   
var func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });    
  
// Using the value() method
let val = _.isFunction(_.bar);
  
// Display the output 
console.log(val);

Output:

false

Example 3:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
  
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
   
var func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });    
  
// Using the value() method
let val = func.isFunction(func.foo);
  
// Display the output 
console.log(val);

Output:

false

Example 4:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
  
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
   
var func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });    
  
// Using the value() method
let val = func.isFunction(func.bar);
  
// Display the output 
console.log(val);

Output:

true

My Personal Notes arrow_drop_up
Last Updated : 23 Sep, 2020
Like Article
Save Article
Similar Reads
Related Tutorials