Open In App

Lodash _.runInContext() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

_.runInContext( context );

Parameters:

  • 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: In this example, we are getting foo and bar functions by the use of the _.runInContext() method.

javascript




// 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.

javascript




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

Last Updated : 03 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads