企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## runInContext + [link](./runInContext "Link to this entry.") + [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L1300 "View in source.") + [npm](https://www.npmjs.com/package/lodash.runincontext "See the npm package.") ``` _.runInContext([context=root]) ``` 创建一个给定上下文对象的原始的 `lodash` 函数。 ### 参数 1. [context=root] (Object) 上下文对象 ### 返回值 (Function) 返回新的 `lodash` 对象 ### 示例 ``` _.mixin({ 'foo': _.constant('foo') }); var lodash = _.runInContext(); lodash.mixin({ 'bar': lodash.constant('bar') }); _.isFunction(_.foo); // => true _.isFunction(_.bar); // => false lodash.isFunction(lodash.foo); // => false lodash.isFunction(lodash.bar); // => true // 使用 `context` 模拟 `Date#getTime` 调用 `_.now` var mock = _.runInContext({ 'Date': function() { return { 'getTime': getTimeMock }; } }); // 或者在 Node.js 中创建一个更高级的 `defer` var defer = _.runInContext({ 'setTimeout': setImmediate }).defer; ```