Open In App

Lodash _.runInContext() Method

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

Syntax:

_.runInContext( context );

Parameters:

Return Value:

This method returns a new lodash function.



Example 1: In this example, we are getting foo and bar functions by the use of the _.runInContext() method.




// Requiring the lodash library
const _ = require("lodash");
 
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
 
let func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });
 
// Check if 'foo' and 'bar' are Lodash functions
let fooFun = _.isFunction(_.foo);
let barFunc = _.isFunction(func.bar);
 
// Display the output
console.log(fooFun);
console.log(barFunc);

Output:



true
true

Example 2: In this example, we are getting a bar functions by the use of the _.runInContext() method.




// Requiring the lodash library
const _ = require("lodash");
 
// Creating an object variable
_.mixin({ 'foo': _.constant('foo') });
 
let func = _.runInContext();
func.mixin({ 'bar': func.constant('bar') });
 
// Check if 'bar' is a function
// in the custom Lodash context
let val = func.isFunction(func.bar);
 
// Display the output
console.log(val); // Outputs: true

Output:

true
Article Tags :