Lodash _.runInContext() Method
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
Please Login to comment...