MooTools Hash/Object.each equivalent in jquery
initialize : function (form, options){
if (this.form = $(form)) {
this.form.isValid = true;
this.regex = ['length'];
this.alertMsg = [];
this.validations = [];
var regex = new Hash(this.options.regexp);
regex.each(function(el, key) {
this.regex.push(key);
}, this);
var alertMsg = new Hash(this.options.alerts);
alertMsg.each(function(el, key) {
this.alertMsg.push(key);
}, this);
$(form).find("*[class*=validate]").each(function(el) {
this.register(el);
}, this);
}
},
......
this.register(el) function is not triggering.....
What "new Hash()" equivalent in Jquery? how to replace function(el) in jquery?
Please help me in this...
Thanks,
HGthe Hash type
in mootools is basically, a prototyped Object clone type
that safely gets methods like .each
, .some
, etc - http://mootools.net/docs/more/Types/Hash
ultimately, you are working with javascript and you can walk objects in javascript with the for var in object
epression:
var object = {
foo: "bar",
bar: "foo"
};
for (var key in object) {
alert(key);
alert(object[key]);
}
things to consider: check for hasOwnProperty or set __proto__: null
on the object to avoid getting problems like these:
Object.prototype.hello = "world";
var object = {
foo: "bar",
bar: "foo"
};
for (var key in object) {
alert(key); // foo, bar, hello
alert(object[key]);
}
instead, do:
var object = {
__proto__: null,
foo: "bar"
};
This can also work as new Object(null)
if available.
And yes, prototyping object is not a safe practice - hence mootools was doing it in Hash and not the actual Object type. Hash is now deprecated in favour of Object
- http://mootools.net/docs/core/Types/Object
精彩评论