开发者

MooTools Hash/Object.each equivalent in jquery

In my project I need to convert a big script(form validation plugin) from mootools to开发者_StackOverflow社区 Jquery. Its has oops concept in it. I searched a lot and found HJS object oriented Jquery plugin and incorporated but having problem in the following...

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,

HG


the 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜