Lodash - runInContext 方法



語法

_.runInContext([context=root])

使用 context 物件建立一個新的原始 lodash 函式。

引數

  • [context=root] (物件) − context 物件。

輸出

  • (函式) − 返回一個新的 lodash 函式。

示例

var _ = require('lodash');

_.mixin({ 'foo': _.constant('foo') });
 
var lodash = _.runInContext();
lodash.mixin({ 'bar': lodash.constant('bar') });
 
console.log(_.isFunction(_.foo));
console.log(_.isFunction(_.bar));
console.log(lodash.isFunction(lodash.foo));
console.log(lodash.isFunction(lodash.bar));

將上面程式儲存在tester.js中。執行以下命令來執行此程式。

命令

\>node tester.js

輸出

true
false
false
true
lodash_util.htm
廣告
© . All rights reserved.