Prototype Hash对象 学习_prototype

复制代码 代码如下:

//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对象

时间: 2024-10-26 19:51:32

Prototype Hash对象 学习_prototype的相关文章

Prototype Object对象 学习_prototype

Object is used by Prototype as a namespace; that is, it just keeps a few new methods together, which are intended for namespaced access (i.e. starting with "Object."). 上面说的namespace个人理解就相当于C#中的静态类,提供工具函数的意思,和C#中的namespace应该不是一个概念.因为C#中的命名空间后面不会直

Prototype Template对象 学习_prototype

复制代码 代码如下: var Template = Class.create({ //初始化方法 initialize: function(template, pattern) { this.template = template.toString(); this.pattern = pattern || Template.Pattern; }, //格式化方法,如果从java的角度来说,其实叫format更好 :) evaluate: function(object) {     //检查是否

Prototype Selector对象学习_prototype

复制代码 代码如下: function $$() { return Selector.findChildElements(document, $A(arguments)); } 这个类可以分成三个部分:第一个部分就是根据不同的浏览器,判断使用什么DOM操作方法.其中操作IE就是用普通的getElementBy* 系列方法:FF是document.evaluate:Opera和Safari是selectorsAPI.第二部分是对外提供的基本函数,像findElements,match等,Eleme

Prototype Class对象学习_prototype

复制代码 代码如下: /* Based on Alex Arnell's inheritance implementation. */ var Class = (function() { //临时存储parent的prototype function subclass() {}; //创建类的方法 function create() { var parent = null, properties = $A(arguments);     //检查新建一个类时,是否指定了一个父对象     //如

Prototype ObjectRange对象学习_prototype

Ranges represent an interval of values. The value type just needs to be "compatible," that is, to implement a succ method letting us step from one value to the next (its successor). Prototype provides such a method for Number and String, but you

Prototype Function对象 学习_prototype

这个对象就是对function的一些扩充,最重要的当属bind方法,prototype的帮助文档上特意说了一句话:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在类继承机制里面就是利用wrap方法来调用父类的同名方法.argumentNames bind bindAsEventListener curry defer delay methodize wrap 复制代码 代码如下: //通

Prototype Array对象 学习_prototype

复制代码 代码如下: Array.from = $A; (function() { //Array原型的引用     var arrayProto = Array.prototype, slice = arrayProto.slice,      //JS 1.6里面会有原生的forEach方法 _each = arrayProto.forEach; // use native browser JS 1.6 implementation if available function each(it

Prototype Date对象 学习_prototype

看一下源码: 复制代码 代码如下: Date.prototype.toJSON = function() { return '"' + this.getUTCFullYear() + '-' + (this.getUTCMonth() + 1).toPaddedString(2) + '-' + this.getUTCDate().toPaddedString(2) + 'T' + this.getUTCHours().toPaddedString(2) + ':' + this.getUTCM

Prototype PeriodicalExecuter对象 学习_prototype

This is a simple facility for periodical execution of a function. This essentially encapsulates the native clearInterval/setInterval mechanism found in native Window objects. This is especially useful if you use one to interact with the user at given