Lodash _.runInContext() Method
Last Updated :
03 Nov, 2023
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
const _ = require( "lodash" );
_.mixin({ 'foo' : _.constant( 'foo' ) });
let func = _.runInContext();
func.mixin({ 'bar' : func.constant( 'bar' ) });
let fooFun = _.isFunction(_.foo);
let barFunc = _.isFunction(func.bar);
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
const _ = require( "lodash" );
_.mixin({ 'foo' : _.constant( 'foo' ) });
let func = _.runInContext();
func.mixin({ 'bar' : func.constant( 'bar' ) });
let val = func.isFunction(func.bar);
console.log(val);
|
Output:
true
Share your thoughts in the comments
Please Login to comment...