排列具有相同数据的多个对象(减少冗余)

时间:2012-07-23 11:08:02

标签: javascript jquery

我正在开发一个Web应用程序,我使用 jquery验证

$("#register").validate({
    rules: {
        emailID: {
            required: true,
            email: true
        },
        pass: {
            required: true,
            minlength: 6
        },
        user_email: {
            required: true,
            email: true
        },
        user_pass: {
            required: true,
            minlength: 6
        }
    }

一切正常,但问题是对象emailIDuser_email两者的{strong>相同数据对{{1}也相同}和pass,我怎样才能将它们排成一行而不更改html中的user_pass属性,如下所示:

name

我知道这种结构不正确。

因为我有很多对象拥有相同的数据,所以在重复

中似乎不太好

2 个答案:

答案 0 :(得分:2)

function RequiredRule( email, minlength ) {
    this.email = email;
    this.minlength = minlength;
    this.required = true;
}

$("#register").validate({
    rules: {
        emailID: new RequiredRule(true),
        pass: new RequiredRule(false, 6),
        user_email: new RequiredRule(true),
        user_pass: new RequiredRule(false, 6)
    }
});

答案 1 :(得分:1)

var one = {
    required: true,
    email: true
},
    two = {
        required: true,
        minlength: 6
};

$("#register").validate({
    rules: {
        emailID: one,
        pass: two,
        user_email: one,
        user_pass: two
    }
});