|
//Hash對象的工具函數
function $H(object) {
return new Hash(object);
};
var Hash = Class.create(Enumerable, (function() {
//初始化,創建一個新的Hash對象
function initialize(object) {
this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
}
//覆蓋Enumerable里面的方法,遍歷Hash對象時會用到
function _each(iterator) {
for (var key in this._object) {
var value = this._object[key], pair = [key, value];
pair.key = key;
pair.value = value;
iterator(pair);
}
}
function set(key, value) {
return this._object[key] = value;
}
function get(key) {
if (this._object[key] !== Object.prototype[key])
return this._object[key];
}
function unset(key) {
var value = this._object[key];
delete this._object[key];
return value;
}
function toObject() {
return Object.clone(this._object);
}
function keys() {
return this.pluck('key');
}
function values() {
return this.pluck('value');
}
//返回value的key
function index(value) {
var match = this.detect(function(pair) {
return pair.value === value;
});
return match && match.key;
}
function merge(object) {
return this.clone().update(object);
}
//更新原有Hash對象,把object參數內的鍵值對更新到原Hash對象
function update(object) {
return new Hash(object).inject(this, function(result, pair) {
result.set(pair.key, pair.value);
return result;
});
}
function toQueryPair(key, value) {
if (Object.isUndefined(value)) return key;
return key + '=' + encodeURIComponent(String.interpret(value));
}
function toQueryString() {
return this.inject([], function(results, pair) {
var key = encodeURIComponent(pair.key), values = pair.value;
if (values && typeof values == 'object') {
if (Object.isArray(values))
return results.concat(values.map(toQueryPair.curry(key)));
} else results.push(toQueryPair(key, values));
return results;
}).join('&');
}
function inspect() {
return '#<Hash:{' + this.map(function(pair) {
return pair.map(Object.inspect).join(': ');
}).join(', ') + '}>';
}
function toJSON() {
return Object.toJSON(this.toObject());
}
function clone() {
return new Hash(this);
}
return {
initialize: initialize,
_each: _each,
set: set,
get: get,
unset: unset,
toObject: toObject,
toTemplateReplacements: toObject,
keys: keys,
values: values,
index: index,
merge: merge,
update: update,
toQueryString: toQueryString,
inspect: inspect,
toJSON: toJSON,
clone: clone
};
})());
Hash.from = $H;
clone
each
get
inspect
keys
merge
remove
set
toJSON
toObject
toQueryString
unset
update
value
給出一些方法的示例,簡單的方法就略過了
toQueryString():
復制代碼 代碼如下:
$H({ action: 'ship', order_id: 123, fees: ['f1', 'f2'], 'label': 'a demo' }).toQueryString()
// -> 'action=ship&order_id=123&fees=f1&fees=f2&label=a%20demo'
// an empty hash is an empty query string:
$H().toQueryString()
// -> ''
update():
復制代碼 代碼如下:
var h = $H({ name: 'Prototype', version: 1.5 });
h.update({ version: 1.6, author: 'Sam' }).inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>
h.inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>
//注意這里會改變原來的Hash對象
merge():
復制代碼 代碼如下:
var h = $H({ name: 'Prototype', version: 1.5 });
h.merge({ version: 1.6, author: 'Sam' }).inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>
h.inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.5}>
//注意這里不改變原有Hash對象
JavaScript技術:Prototype Hash對象 學習,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。