Reputation: 499
mootools 1.2.5 doc says: A custom Object ({}) implementation which does not account for prototypes when setting, getting, or iterating. Useful because in JavaScript, we cannot use Object.prototype. Instead, we can use Hash.prototype!
But I don't understand it, please give me some example to show how can we benefit from Hash.
Upvotes: 1
Views: 455
Reputation: 708
The reason Hash existed is because implementing method is Object.prototype is very very bad practice because it will break your application.
Hash made it possible to use Hash.prototype, for example:
new Hash({my: 'nice', literal: 'object'}).filter(someFunction).map(function(value){
return value + ' sfsdf ';
}).getValues();
You could use Hash.implement
to add methods of your own, which actually adds methods to Hash.prototype.
Because with the new ECMAScript 5 specification, which has lots of Object.* functions, like Object.keys, MooTools 1.3 makes now use of those functions instead of Hash.
var obj = {my: 'nice', literal: 'object'}
Object.values(Object.map(Object.filter(obj, someFunction), function(value){
return value + ' sfsdf ';
}));
Maybe a little bit more verbose in this example, but more in line with ES5. If you really like Hash, you could still use them in MooTools More 1.3.
Note that you can use Hash exactly like the Object example above, so:
var values = Hash.getValues({my: 'nice', literal: 'object'});
Upvotes: 0
Reputation: 26165
@bowsersenior has provided examples that are from 1.3 where mootools deprecated Hash in favour of extending the Object type but since you talk of the 1.2.5 docs, I will reply anyway.
The most useful thing about Hash in 1.2.x was the ability to create Object-like structures that can be prototyped.
I replied a while back on the subject giving similar examples: Is there an jQuery equivalent of MooTools Hash?
When it comes to 1.3 w/o compatibility mode, you can still define custom Object methods via Object.implement()
instead but it's slightly different in that this
is not the object and will always require a call to the Object.yourCustomMethod(yourobj, callbackFunction)
.
Upvotes: 0
Reputation: 12574
First, Hash
has been deprecated in favor of Object
in MooTools 1.3 .
MooTools Object
adds some very helpful methods to Object
. The info about prototypes
is relevant for advanced use, but you don't need to worry about it for most cases when you use MooTools Object
.
Here are some of the useful methods MooTools adds to Object
:
// each
// alerts 'The first day of the week is Sunday',
// 'The second day of the week is Monday', etc.:
Object.each(
{first: 'Sunday', second: 'Monday', third: 'Tuesday'},
function(value, key){
alert('The ' + key + ' day of the week is ' + value);
});
// map
var myObject = {a: 1, b: 2, c: 3};
var timesTwo = Object.map(timesTwo, function(value, key){
return value * 2;
}); // timesTwo now holds an object containing: {a: 2, b: 4, c: 6};
// filter
var myObject = {a: 10, b: 20, c: 30};
var biggerThanTwenty = Object.filter(myObject, function(value, key){
return value > 20;
}); // biggerThanTwenty now holds an object containing: {c: 30}
(examples above and many more from MooTools docs)
Upvotes: 1