Lo-dash扩展/分配默认行为

时间:2014-01-15 22:02:52

标签: javascript lodash

我正在尝试理解lodash中的assign / extend函数,因为我通过mean.io堆栈设置遇到了一个非常令人困惑的结果。

下面:

'use strict';

var _ = require('lodash');

// Load app configuration
var foo = require(__dirname + '/../config/env/all.js');
console.log('-.-.-.-.-.-.-.-.-.-. foo before _.assign .-.-.-.-.-.-.-.-.-.-');
console.log(foo)

module.exports = _.assign(
    require(__dirname + '/../config/env/all.js'),
    require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {}
    );

var bar = require(__dirname + '/../config/env/all.js');

console.log('-.-.-.-.-.-.-.-.-.-. foo after _.assign .-.-.-.-.-.-.-.-.-.-');
console.log(foo);
console.log('-.-.-.-.-.-.-.-.-.-. bar .-.-.-.-.-.-.-.-.-.-');
console.log(bar);
console.log('-.-.-.-.-.-.-.-.-.-. A direct require call .-.-.-.-.-.-.-.-.-.-');
console.log(require(__dirname + '/../config/env/all.js'));

我得到的是:

-.-.-.-.-.-.-.-.-.-. foo before _.assign .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
  port: 3000,
  db: undefined }
-.-.-.-.-.-.-.-.-.-. foo after _.assign .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
  port: 3000,
  db: 'mongodb://localhost/mean-dev',
  app: { name: 'MEAN - A Modern Stack - Development' },
  facebook: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/facebook/callback' },
  twitter: 
   { clientID: 'CONSUMER_KEY',
     clientSecret: 'CONSUMER_SECRET',
     callbackURL: 'http://localhost:3000/auth/twitter/callback' },
  github: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/github/callback' },
  google: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/google/callback' } }
-.-.-.-.-.-.-.-.-.-. bar .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
  port: 3000,
  db: 'mongodb://localhost/mean-dev',
  app: { name: 'MEAN - A Modern Stack - Development' },
  facebook: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/facebook/callback' },
  twitter: 
   { clientID: 'CONSUMER_KEY',
     clientSecret: 'CONSUMER_SECRET',
     callbackURL: 'http://localhost:3000/auth/twitter/callback' },
  github: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/github/callback' },
  google: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/google/callback' } }
-.-.-.-.-.-.-.-.-.-. A direct require call .-.-.-.-.-.-.-.-.-.-
{ root: '/home/bax/Code/lab/mean/test1',
  port: 3000,
  db: 'mongodb://localhost/mean-dev',
  app: { name: 'MEAN - A Modern Stack - Development' },
  facebook: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/facebook/callback' },
  twitter: 
   { clientID: 'CONSUMER_KEY',
     clientSecret: 'CONSUMER_SECRET',
     callbackURL: 'http://localhost:3000/auth/twitter/callback' },
  github: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/github/callback' },
  google: 
   { clientID: 'APP_ID',
     clientSecret: 'APP_SECRET',
     callbackURL: 'http://localhost:3000/auth/google/callback' } }
Express app started on port 3000

问题是:每次我都不应该得到相同的结果:console.log(require(__dirname + '/../config/env/all.js'));

1 个答案:

答案 0 :(得分:0)

这已经在评论中得到了解答,但是这里尝试了更合格的答案并关闭了这个主题:

您获得的效果是由于node.js模块加载和lodash _.extend()的综合效果:

  • 包含的模块只是对象,它们被缓存,因此每次后续调用都需要()'与相同的物理文件系统路径相关的将返回该对象
  • Lodash的扩展/分配是令人讨厌的破坏性的,它直接操纵传递的对象而不是返回一个新对象(例如Rubys' Hash#merge()'会做什么)

所以你有一个代表all.js_.assign()的对象操纵它。任何进一步的require()将只返回相同的修改对象。

如果这是不受欢迎的(在很多情况下都是如此),到目前为止我所知道的唯一解决方法就是:

var someOptions = {/*...*/};
var extended = _.extend(_.clone(someOptions))