您的位置:首页 > 理论基础 > 数据结构算法

从fis中得来的数据结构,Object版,

2017-01-10 15:29 274 查看
/*
* 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: