fis中的数据结构模块Config
2017-01-10 15:17
183 查看
/* * config * caoke */ 'use strict'; Object.extend=function(props){ //继承父类 var prototype=Object.create(this.prototype) //初始化函数ctor var _Class=function(){ if(arguments.length>0){ var sp=new _Class() if (sp.constructor){ sp.constructor.apply(sp, arguments); } return sp; } } //当前类属性和方法 for(var k in props){ prototype[k]= props[k] } _Class.prototype = prototype; //类继承 _Class.extend=this.extend; //类扩展 _Class.expand = function (prop) { for (var name in prop) { prototype[name] = prop[name]; } }; return _Class } //You can't use merge in util.js function merge(source, target){ if(typeof source === 'object' && typeof target === 'object'){ for(var key in target){ if(target.hasOwnProperty(key)){ source[key] = merge(source[key], target[key]); } } } else { source = target; } return source; } var Config = Object.extend({ constructor : function(){ this.init.apply(this, arguments); }, init : function(){ this.data = {}; if(arguments.length > 0){ this.merge.apply(this, arguments); } return this; }, get : function(path, def){ var result = this.data || {}; (path || '').split('.').forEach(function(key){ if(key && (typeof result !== 'undefined')){ result = result[key]; } }); if(typeof result === 'undefined'){ return def; } else { return result; } }, set : function(path, value){ if(typeof value === 'undefined'){ this.data = path; } else { path = String(path || '').trim(); if(path){ var paths = path.split('.'), last = paths.pop(), data = this.data || {}; paths.forEach(function(key){ var type = typeof data[key]; if(type === 'object'){ data = data[key]; } else if(type === 'undefined'){ data = data[key] = {}; } else { console.error('forbidden to set property[' + key + '] of [' + type + '] data'); } }); data[last] = value; } } return this; }, del : function(path){ path = String(path || '').trim(); if(path){ var paths = path.split('.'), data = this.data, last = paths.pop(), key; for(var i = 0, len = paths.length; i < len; i++){ key = paths[i]; if(typeof data[key] === 'object'){ data = data[key]; } else { return this; } } if(typeof data[last] !== 'undefined'){ delete data[last]; } } return this; }, merge : function(){ var self = this; [].slice.call(arguments).forEach(function(arg){ if(typeof arg === 'object'){ merge(self.data, arg); } else { console.warning('unable to merge data[' + arg + '].'); } }); return this; } }); module.exports = Config;
相关文章推荐
- Python ConfigParser模块常用方法示例
- nginx-http模块的数据结构
- ConfigParser模块教程
- Linux netfilter 学习笔记 之七 ip层netfilter的连接跟踪模块的概念及相关的数据结构分析
- ConfigParser模块详解
- 浅析Python heapq模块 堆数据结构
- Dojo初探之2:设置dojoConfig详解,dojoConfig参数详解+Dojo中预置自定义AMD模块的四种方式(基于dojo1.11.2)
- Ceph网络模块(2) - AsyncMessenger数据结构分析
- ConfigParser模块简明教程
- 如何写一个给自己的框架写一个优雅的Java Config模块
- drupal7为admin/config页面添加自己开发的模块
- Python自动化测试 ConfigParser模块读写配置文件
- ConfigParser模块学习: 用python解析配置文件
- Python模块之ConfigParser - 读写配置文件
- asp.net webconfig下的httphandler模块配置
- 数据结构之模块二:非线性结构-树结构
- python-logging.config【日志模块】
- Python模块: ConfigParser
- Qualcomm摄像头模块中的一些数据结构
- configparser模块(拷贝)