Lodash _.runInContext

2021-09-23 17:25 更新

_.runInContext([context=root]) 

創(chuàng)建一個(gè)給定context上下文對(duì)象的原始的 lodash 函數(shù)。

添加版本

1.1.0

參數(shù)

  1. [context=root] (Object): 上下文對(duì)象。

返回

(Function): 返回新的 lodash 對(duì)象

例子

_.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` 調(diào)用 `_.now`
var stubbed = _.runInContext({  'Date': function() {    return { 'getTime': stubGetTime };  }}); 
// 或者在 Node.js 中創(chuàng)建一個(gè)更高級(jí)的 `defer`
var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)