Hash is a data type extended by the prototype author. In essence, it is a common JavaScript Object (Note: Do not tangle with any JavaScript variables that are objects. Here we will talk about new object (), and then expand some other methods on this object.
Basic Principles
The Code Description of the basic principle is:
function Hash(object){
this._object = object;
}
Hash.prototype = {
constructor : Hash,
method_1 : function(){//this._object},
method_2 : function(){//this._object}
//...
}
var hash_1 = new Hash({name : 'xesam'});
However, this is definitely not the case in the source code. This is not the case in the form of creating classes. Therefore, we should replace it with the form of creating classes in paototype (Note: Prototype source code analysis-class part 1) ):
var Hash = Class.create(Enumerable, (function() {
function initialize() {
//this._object
}
return {
initialize : initialize
}
})())
The enumerable object is also incorporated into the hash to add more methods to the hash.
(Here we have a difference: Although both hash and array are mixed into enumerable, what is different from array is that array is a native data type of Js, only the extend method can be used. Hash is a data type created by the author. Therefore, the hash method is class. create method .)
Since the enumerable object is mixed in, there must be a _ each method. For the hash object, the iterator parameter of the _ each method is a pair, which is more special.
It is both an array [key, pair] and an object:
{
key : key,
value : value
}
Therefore, the pair [0] And pair. Key results are consistent.
Basic operations:
Take a look at the initialize method when a class is created. Each hash object maintains an internal object. The variable name is _ object, therefore, in principle, you cannot directly read or set instance attributes by operating on instance attributes. Therefore, the author provides a separate method. Now there are three basic methods: Get, set, and unset.
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;
}
(Note: Prototype classes are very basic and basically not encapsulated. Therefore, although a _ object variable is maintained internally, it is not a real private variable, you can use the following get, set, and unset attributes to access a hash attribute, or directly modify the _ OBJECT object:
hash.set('name','new name')
hash._object.name = 'new name'
There is no difference in the above operations, just be conscious .)
Copy hash:
First:
Clone the current hash object (hash) -- clone. Clone an internal object (_ OBJECT) -- toobject
Toobject calls the object. Clone method. Now let's review the object. Clone method.
function clone(object) {
return extend({ }, object);
}
This. _ object is copied to an empty object and then returned.
function clone() {
return new Hash(this);
}
Now let's take a look at the initialize function when creating the hash class. the initialization process of this. _ object is ignored previously:
function initialize(object) {
this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
}
Let's look at the implementation: if an object is a common object, you can directly call the object. Clone method. If an object is a hash instance, call object. toobject to pay attention to the nature of object. toobject. The object. Clone method is called indirectly. However, the type returned is the hash type, and various hash methods are available.
Obtains the keys and values arrays of hash objects.
The method names are keys and values. Here we use a method of the parent object -- plunk (Note: plunk is a method defined in enumerable prototype source code analysis -- enumerable part (3 )):
function keys() {
return this.pluck('key');
}
function values() {
return this.pluck('value');
}
The most closely related method to plunk is each. Let's take a look at each. The iterator parameter of the each function is pair, an object.
Update (merge) a hash instance:
There are two types of operations: one is destructive Update (the original object will be modified) and the other is non-destructive Merge (the original object will not be modified ).
Update uses the inject method (Note: Prototype source code analysis-enumerable part (3). The first parameter is this (current hash instance), so the current object will be modified.
Merge uses the update method, but the clone operation is performed first, so the original hash instance is retained.
Function Merge (object ){
Return this. Clone (). Update (object); // The Internal Variable _ object is copied first.
}
Function Update (object ){
Return new hash (object). Inject (this, function (result, pair ){
Result. Set (pair. Key, pair. value );
Return result;
});
}
Format conversion:
Toquerypair and toquerystring
Toquerypair is an internal method used to check the value. If the value is not undefined, only the key value is retained. If the value is null, the key = format is retained. Otherwise, the value is converted to the Key = encodeuricomponent (value) format.
function toQueryPair(key, value) {
if (Object.isUndefined(value)) return key;
return key + '=' + encodeURIComponent(String.interpret(value));
}
Toquerystring mainly involves the case where values is an array. If values is an array, use Concat to expand it. If values is a string, push it directly before expanding it.
Here the methods are the same as those of object. toquerystring, which can be replaced by each other. Here we will go back to the STR method in the object. That is clear. Let's just talk about it (Note: Prototype source code analysis-object part (3) about JSON ).
Inspect has nothing to say. Very common.
For more information, see http://www.cnblogs.com/xesam /]
Address: http://www.cnblogs.com/xesam/archive/2012/01/31/2332918.html