0
我試圖瞭解lodash中的賦值/擴展函數,因爲我碰到了一個非常混亂的結果,通過mean.io棧設置。Lo-dash擴展/分配默認行爲
這裏:
'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'));
?
你得到相同的對象,但你已經修改了它之間? – Bergi
嗯,哦。我想我明白了。我從require()得到的是對由_.assign()修改的module.exports對象的引用,所以當我再次調用require()時,我得到對同一個修改模塊導出對象的引用...是這個對? –
是的,正好:-) – Bergi