• Javascript模式(三) 策略模式


    var data = {
            "username" : "zhangsan",
            "password" : "12345690",
            "code" : "abcd"
        };
    
        var validate = {
            rules : {},
            config : {},
            msg : [],
            check : function(data){
                var k, rule, config, checker;
                rule = this.rules;
                config = this.config;
                this.msg.length = 0;
                for(k in data){
                    if(data.hasOwnProperty(k)){
                        if(!rule[k]){
                            throw new Error("validate.check error, error type: " + k + " not exist");
                        }
                        checker = this.rules[k];
                        if(!checker.validate(data[k])){
                            this.msg.push(checker.msg);
                        }
                    }
                }
            }
        };
    
        validate.rules.username = {
            validate : function(username){
                return username.length > 7;
            },
            msg : "用户名有误"
        };
    
        validate.rules.password = {
            validate : function(password){
                return password.length > 7
            },
            msg : "密码挂了"
        };
    
        validate.rules.code = {
            validate : function(code){
                return code === "abcdef";
            },
            msg : "验证码不对"
        };
    
        validate.check(data);
    
        if(validate.msg.length){
            console.log(validate.msg.join("
    "));
        }
  • 相关阅读:
    一,安装python
    maven搭建ssm
    web优化
    java代码优化29个点
    供参考的 php 学习路线
    javascript-文档结构遍历
    jquery中的cookie使用
    jQuery中的Ajax
    lambda和抽象类
    上传jar包到nexus私服
  • 原文地址:https://www.cnblogs.com/mr189/p/3964489.html
Copyright © 2020-2023  润新知