[转帖]Mootools源码分析-03 -- Hash
生活随笔
收集整理的這篇文章主要介紹了
[转帖]Mootools源码分析-03 -- Hash
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//哈希表,Native化作為內(nèi)置對象
var Hash =new Native({
//族名,類名,用于$type方法的精準類型判斷
name: 'Hash',
initialize: function(object) {
//對于哈希表的實例,復制副本以解除兩者的引用/鏈接關系
if ($type(object) =='hash') ōbject = $unlink(object.getClean());
//再復制到當前實例
for (var key in object) this[key] = object[key];
returnthis;
}
});
//對哈希表的擴展的實現(xiàn)
Hash.implement({
//哈希表長度(鍵值數(shù)統(tǒng)計,不包括值型為函數(shù)的)
getLength: function() {
var length =0;
for (var key inthis) {
//hasOwnProperty判斷是否具有指定名稱的屬性,注意是屬性,不包括方法
if (this.hasOwnProperty(key)) length++;
}
return length;
},
//迭代,同時是只遍歷屬性,這個也很符合實際使用的情況
forEach: function(fn, bind) {
for (var key inthis) {
if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
}
},
//復制一個副本,不知道為什么用這個不容易理解的名字
getClean: function() {
var clean = {};
for (var key inthis) {
if (this.hasOwnProperty(key)) clean[key] =this[key];
}
return clean;
}
});
//將each作為forEach的別名
Hash.alias('forEach', 'each');
//轉(zhuǎn)為哈希表的快捷方式
function $H(object) {
returnnew Hash(object);
};
//額外的擴展
Hash.implement({
//hasOwnProperty的快捷方式
has: Object.prototype.hasOwnProperty,
//根據(jù)值找對應的鍵
keyOf: function(value) {
for (var key inthis) {
if (this.hasOwnProperty(key) &&this[key] === value) return key;
}
returnnull;
},
//查找是否存在指定值
hasValue: function(value) {
return (Hash.keyOf(this, value) !==null);
},
//數(shù)據(jù)擴展,會覆蓋原數(shù)據(jù)
extend: function(properties) {
Hash.each(properties, function(value, key) {
Hash.set(this, key, value);
}, this);
returnthis;
},
//合并,不包括重復項
combine: function(properties) {
Hash.each(properties, function(value, key) {
Hash.include(this, key, value);
}, this);
returnthis;
},
//探險指定鍵值
erase: function(key) {
if (this.hasOwnProperty(key)) deletethis[key];
returnthis;
},
//根據(jù)鍵名讀取值
get: function(key) {
return (this.hasOwnProperty(key)) ?this[key] : null;
},
//設置鍵-值
set: function(key, value) {
if (!this[key] ||this.hasOwnProperty(key)) this[key] = value;
returnthis;
},
//清空哈希表
empty: function() {
Hash.each(this, function(value, key) {
deletethis[key];
}, this);
returnthis;
},
//包含新鍵-值,如果舊數(shù)據(jù)中不存在該鍵名
include: function(key, value) {
var k =this[key];
if (!$defined(k)) this[key] = value;
returnthis;
},
//類似Array對象的map方法,區(qū)別在于傳給fn方法的參數(shù)
map: function(fn, bind) {
var results =new Hash;
Hash.each(this, function(value, key) {
results.set(key, fn.call(bind, value, key, this));
}, this);
return results;
},
//類似Array對象的filter方法,區(qū)別在于傳給fn方法的參數(shù)
filter: function(fn, bind) {
var results =new Hash;
Hash.each(this, function(value, key) {
if (fn.call(bind, value, key, this)) results.set(key, value);
}, this);
return results;
},
//類似Array對象的every方法,區(qū)別在于傳給fn方法的參數(shù)
every: function(fn, bind) {
for (var key inthis) {
if (this.hasOwnProperty(key) &&!fn.call(bind, this[key], key)) returnfalse;
}
returntrue;
},
//類似Array對象的some方法,區(qū)別在于傳給fn方法的參數(shù)
some: function(fn, bind) {
for (var key inthis){
if (this.hasOwnProperty(key) && fn.call(bind, this[key], key)) returntrue;
}
returnfalse;
},
//獲取哈希表中的所有鍵,以數(shù)組返回
getKeys: function() {
var keys = [];
Hash.each(this, function(value, key) {
keys.push(key);
});
return keys;
},
//獲取哈希表中的所有值,以數(shù)組返回
getValues: function() {
var values = [];
Hash.each(this, function(value) {
values.push(value);
});
return values;
},
//將哈希表數(shù)據(jù)轉(zhuǎn)為表單提交形式的查詢串
toQueryString: function(base) {
var queryString = [];
Hash.each(this, function(value, key) {
if (base) key = base +'['+ key +']';
var result;
switch ($type(value)) {
case'object':
result = Hash.toQueryString(value, key);
break;
case'array':
var qs = {};
value.each(function(val, i) {
qs[i] = val;
});
result = Hash.toQueryString(qs, key);
break;
default:
result = key +'='+ encodeURIComponent(value);
}
if (value != undefined) queryString.push(result);
});
return queryString.join('&');
}
});
//分別為keyOf和hasValue建立indexOf和contains的別名方法
Hash.alias({keyOf: 'indexOf', hasValue: 'contains'});
var Hash =new Native({
//族名,類名,用于$type方法的精準類型判斷
name: 'Hash',
initialize: function(object) {
//對于哈希表的實例,復制副本以解除兩者的引用/鏈接關系
if ($type(object) =='hash') ōbject = $unlink(object.getClean());
//再復制到當前實例
for (var key in object) this[key] = object[key];
returnthis;
}
});
//對哈希表的擴展的實現(xiàn)
Hash.implement({
//哈希表長度(鍵值數(shù)統(tǒng)計,不包括值型為函數(shù)的)
getLength: function() {
var length =0;
for (var key inthis) {
//hasOwnProperty判斷是否具有指定名稱的屬性,注意是屬性,不包括方法
if (this.hasOwnProperty(key)) length++;
}
return length;
},
//迭代,同時是只遍歷屬性,這個也很符合實際使用的情況
forEach: function(fn, bind) {
for (var key inthis) {
if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
}
},
//復制一個副本,不知道為什么用這個不容易理解的名字
getClean: function() {
var clean = {};
for (var key inthis) {
if (this.hasOwnProperty(key)) clean[key] =this[key];
}
return clean;
}
});
//將each作為forEach的別名
Hash.alias('forEach', 'each');
//轉(zhuǎn)為哈希表的快捷方式
function $H(object) {
returnnew Hash(object);
};
//額外的擴展
Hash.implement({
//hasOwnProperty的快捷方式
has: Object.prototype.hasOwnProperty,
//根據(jù)值找對應的鍵
keyOf: function(value) {
for (var key inthis) {
if (this.hasOwnProperty(key) &&this[key] === value) return key;
}
returnnull;
},
//查找是否存在指定值
hasValue: function(value) {
return (Hash.keyOf(this, value) !==null);
},
//數(shù)據(jù)擴展,會覆蓋原數(shù)據(jù)
extend: function(properties) {
Hash.each(properties, function(value, key) {
Hash.set(this, key, value);
}, this);
returnthis;
},
//合并,不包括重復項
combine: function(properties) {
Hash.each(properties, function(value, key) {
Hash.include(this, key, value);
}, this);
returnthis;
},
//探險指定鍵值
erase: function(key) {
if (this.hasOwnProperty(key)) deletethis[key];
returnthis;
},
//根據(jù)鍵名讀取值
get: function(key) {
return (this.hasOwnProperty(key)) ?this[key] : null;
},
//設置鍵-值
set: function(key, value) {
if (!this[key] ||this.hasOwnProperty(key)) this[key] = value;
returnthis;
},
//清空哈希表
empty: function() {
Hash.each(this, function(value, key) {
deletethis[key];
}, this);
returnthis;
},
//包含新鍵-值,如果舊數(shù)據(jù)中不存在該鍵名
include: function(key, value) {
var k =this[key];
if (!$defined(k)) this[key] = value;
returnthis;
},
//類似Array對象的map方法,區(qū)別在于傳給fn方法的參數(shù)
map: function(fn, bind) {
var results =new Hash;
Hash.each(this, function(value, key) {
results.set(key, fn.call(bind, value, key, this));
}, this);
return results;
},
//類似Array對象的filter方法,區(qū)別在于傳給fn方法的參數(shù)
filter: function(fn, bind) {
var results =new Hash;
Hash.each(this, function(value, key) {
if (fn.call(bind, value, key, this)) results.set(key, value);
}, this);
return results;
},
//類似Array對象的every方法,區(qū)別在于傳給fn方法的參數(shù)
every: function(fn, bind) {
for (var key inthis) {
if (this.hasOwnProperty(key) &&!fn.call(bind, this[key], key)) returnfalse;
}
returntrue;
},
//類似Array對象的some方法,區(qū)別在于傳給fn方法的參數(shù)
some: function(fn, bind) {
for (var key inthis){
if (this.hasOwnProperty(key) && fn.call(bind, this[key], key)) returntrue;
}
returnfalse;
},
//獲取哈希表中的所有鍵,以數(shù)組返回
getKeys: function() {
var keys = [];
Hash.each(this, function(value, key) {
keys.push(key);
});
return keys;
},
//獲取哈希表中的所有值,以數(shù)組返回
getValues: function() {
var values = [];
Hash.each(this, function(value) {
values.push(value);
});
return values;
},
//將哈希表數(shù)據(jù)轉(zhuǎn)為表單提交形式的查詢串
toQueryString: function(base) {
var queryString = [];
Hash.each(this, function(value, key) {
if (base) key = base +'['+ key +']';
var result;
switch ($type(value)) {
case'object':
result = Hash.toQueryString(value, key);
break;
case'array':
var qs = {};
value.each(function(val, i) {
qs[i] = val;
});
result = Hash.toQueryString(qs, key);
break;
default:
result = key +'='+ encodeURIComponent(value);
}
if (value != undefined) queryString.push(result);
});
return queryString.join('&');
}
});
//分別為keyOf和hasValue建立indexOf和contains的別名方法
Hash.alias({keyOf: 'indexOf', hasValue: 'contains'});
轉(zhuǎn)載于:https://www.cnblogs.com/pigtail/archive/2011/08/28/2156144.html
總結(jié)
以上是生活随笔為你收集整理的[转帖]Mootools源码分析-03 -- Hash的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Activity的Title中加入进度
- 下一篇: 30分钟正则表达式入门