Home > Web Front-end > JS Tutorial > Prototype Hash object learning_prototype

Prototype Hash object learning_prototype

WBOY
Release: 2016-05-16 18:49:43
Original
1283 people have browsed it
Copy code The code is as follows:

//Utility function of Hash object
function $H(object ) {
return new Hash(object);
};

var Hash = Class.create(Enumerable, (function() {

//Initialization, create a new Hash object
function initialize(object) {
this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
}

/ /Override the methods in Enumerable,
function _each(iterator) {
for (var key in this._object) {
var value = this._object[key], pair will be used when traversing Hash objects = [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');
}

//Return the key of value
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);
}

//Update the original Hash object, update the key-value pair in the object parameter to the original Hash object
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 '#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
Give some examples of methods, simple methods are skipped
toQueryString():
Copy code The code is as follows:

$H({ action: 'ship', order_id: 123, fees: ['f1', 'f2'], 'label': 'a demo' }).toQueryString()
// -> 'action=ship&order_id=123&fees=f1&fees=f2&label=a demo'

// an empty hash is an empty query string:
$H().toQueryString()
// -> ''

update():
Copy code The code is as follows:

var h = $H({ name: 'Prototype', version: 1.5 });
h.update({ version: 1.6, author: 'Sam' }).inspect();
// -> #

h.inspect();
// -> #

//Note that this will change the original Hash object

merge():
Copy code The code is as follows:

var h = $H({ name: 'Prototype', version: 1.5 });
h.merge({ version: 1.6, author: 'Sam' }).inspect();
// -> #

h.inspect();
// -> #

//Note that the original Hash object is not changed here
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template