Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created August 20, 2014 11:52
Show Gist options
  • Save kwatch/5e61cbb6eb20604c273b to your computer and use it in GitHub Desktop.
Save kwatch/5e61cbb6eb20604c273b to your computer and use it in GitHub Desktop.
__proto__ を活用した、環境別設定オブジェクト
// -*- coding: utf-8 -*-
// ベースとなる設定
var config_common = {
name: 'My Application',
mode: null,
secret: null,
db: {
host: 'localhost',
port: 9876,
user: 'user1',
pass: 'pass1'
}
};
// 環境別の設定
var config_development = {
mode: 'development',
secret: '7d1157edff41bf5',
db: {
user: 'devuser',
pass: 'devpass'
}
};
// 環境別の設定にない項目は、ベース設定を参照する
config_development.__proto__ = config_common;
config_development.db.__proto__ = config_common.db;
// 参照結果
var config = config_development;
console.log(config.name); //=> My Application
console.log(config.mode); //=> development # 上書き
console.log(config.secret); //=> 7d1157edff41bf5 # 上書き
console.log(config.db.host); //=> localhost
console.log(config.db.port); //=> 9874
console.log(config.db.user); //=> devuser # 上書き
console.log(config.db.pass); //=> devpass # 上書き
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment