/* * config * caoke */ 'use strict'; //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; } Object.prototype.hget = function(path, def){ var result = this || {}; (path || '').split('.').forEach(function(key){ if(key && (typeof result !== 'undefined')){ result = result[key]; } }); if(typeof result === 'undefined'){ return def; } else { return result; } } Object.prototype.hset = function(path, value){ if(typeof value === 'undefined'){ for(var k in path){ this[k]=path[k]; } } else { path = String(path || '').trim(); if(path){ var paths = path.split('.'), last = paths.pop(), data = this || {}; 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; } Object.prototype.hdel = function(path){ path = String(path || '').trim(); if(path){ var paths = path.split('.'), data = this, 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; } Object.prototype.hmerge = function(){ var self = this; [].slice.call(arguments).forEach(function(arg){ if(typeof arg === 'object'){ merge(self, arg); } else { console.warning('unable to merge data[' + arg + '].'); } }); return this; } var sp={} sp.hset("user.name","caoke") sp.hset("user.qq","914890674") console.log(sp)