如何记录变量/数组/属性包含构造函数?

时间:2018-05-31 15:06:05

标签: javascript node.js jsdoc

有时,我需要使用字符串或数值来决定构造函数,这些构造函数都共享同一个类。

例如,现在我正在实现具有多种代理管理模式的反向代理:TCP,UDP和symethric UDP。

所以我有一个对象:

$(document).ready(function () {
  //hide the other two forms
    $("#store-form").hide();
    $("#account-form").hide();

    //$("#first-next").addClass("disabled");

    //var state2 = $("#state").val();

    /*$("#state").change(function ()
    {
        var state2 = $("#state").val();
        alert(state2);
    })*/

    //alert(state2);

    $.validator.setDefaults({
        errorClass: 'help-block',
        highlight: function (element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function (element) {
            $(element).closest('.form-group').removeClass('has-error');
        }
    })

    $.validator.addMethod('strongPassword', function (value, element) {
        return this.optional(element) || value.length >= 6
        // && /\d/.test(value) && /[a-z]/i.test(value);
    }, 'Your password must be at least 6 characters long');//'Your password must be at least 6 characters long, contain at least on e number and a character');

    //The method for all select options
    $.validator.addMethod('checkSelect', function (value, element) {
        return this.optional(element) || (value != "Please choose...")
        // && /\d/.test(value) && /[a-z]/i.test(value);
    }, 'You must select an option');//'Your password must be at least 6 characters long, contain at least on e number and a character');
$("form").validate({
        rules: {
            firstname: {
                required: true,
                nowhitespace: true,
                lettersonly: true
            },
            lastname: {
                required: true,
                nowhitespace: true,
                lettersonly: true
            },
            phone: {
                required: true,
            },
                    storename: {
                required: true
            },
            state: {
                required: true,
                checkSelect: true
            },
            lga: {
                required: true,
                checkSelect: true
            },
            address: {
                required: true,
            },
            category: {
                required: true,
                checkSelect: true
            },
            description: {
                required: true,
                lettersonly: true
            },
                        email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                strongPassword: true
            },
            password2: {
                required: true,
                equalTo: "#password"
            }
        },
        messages: {
            firstname: {
                required: 'Please enter your first name',
                nowhitespace: 'Please enter a valid first name',
                lettersonly: 'Please enter only alphabets'
            },
            lastname: {
                required: 'Please enter your first name',
                nowhitespace: 'Please enter a valid last name',
                lettersonly: 'Please enter only alphabets'
            },
            phone: {
                required: "Please enter a valid phone no"
            },
            storename: {
                required: "Please enter the name of your store"
            },
            address: {
                required: "Please enter the address of your store"
            },
            description: {
                required: "Briefly descibe what you sell or do"
            },
            email: {
                required: 'Please enter your email address',
                email: "Please enter a valid email address"
            },
            password: {
                required: 'Please enter your password',
                strongPassword: "Your password is not strong enough"
            },
            password2: {
                required: 'Please enter your password',
                equalTo: "Both passwords don't match"
            }
        }
    });


    /*if($("#personal-form").valid() == true)
    {
        $("#first-next").removeClass("disabled");
        alert($("#personal-form").valid());
    }*/

    if ($("#first-next").click(function () {
        if ($("#personal-form :input").valid() == true) {
        //$("#first-next").removeClass("disabled");
        //alert($("#personal-form").valid());

            $("#personal-form").hide();
            $("#store-form").fadeIn();
            $("#account-form").hide();

        //$("#error-msg").html("");
    }
    else {
        //$("#error-msg").html("Please correct all errors before clicking next");
    }
    }));

    /*$("#firstname").on('change', function() {
        if($("#firstname").valid() == true)
        {
            console.log("test");
            alert("It worked");
        }
    });*/

    //$('#firstname').on('input', function()
    //{
    //var firstname = $("#firstname").val();
    //alert(firstname);
    //alert("hello");
    //console.log("test");
    //alert("You just typed something in the firstname field");
    //});


    if ($("#second-next").click(function () {
        if ($("#store-form :input").valid() == true) {
        //$("#first-next").removeClass("disabled");
        //alert($("#personal-form").valid());

            $("#personal-form").hide();
            $("#store-form").hide();
            $("#account-form").fadeIn();

        //$("#error-msg").html("");
    }
    else {
        //$("#error-msg").html("Please correct all errors before clicking next");
    }
    }));

    if ($("#first-previous").click(function () {
        $("#personal-form").fadeIn();
        $("#store-form").hide();
        $("#account-form").hide();

        //$("#error-msg").html("");
    }));

    $("#email").on('input', function () {
        if ($("#email").valid() == true) {
            //console.log("test");
            //alert("It worked");

            var email = $("#email").val();

            $("#check-email-server").load('php/check_email.php', { "email": email });
        }
    });

    $("#email").on('focusout', function () {
        if ($("#email").valid() == true) {
            //console.log("test");
            //alert("It worked");

            var email = $("#email").val();

            $("#check-email-server").load('php/check_email.php', { "email": email });
        }
    });

    if ($("#second-previous").click(function () {
        $("#personal-form").hide();
        $("#store-form").fadeIn();
        $("#account-form").hide();

        //$("#error-msg").html("");
    }));
});

代理将根据先前的初始化请求选择适当的服务器模块。

但是,如何记录const servers = { TCP: ProxyControllerTCP, UDP: ProxyControllerUDP, UDP_sym: UDPProxyBidirectional }; 包含servers基类的构造函数?我需要使用Visual Studio 2017 intellisense。

1 个答案:

答案 0 :(得分:0)

答案包括两件事,这两件事都很难理解。

1。记录对象的值类型

这是记录对象包含的值的语法:

/** @type {[key: string]: number} **/
const myObjOfNumbers = {};

// Visual studio hints number type, even though the value is not defined right now
const num = myObjOfNumbers["hello world"];

2。定义构造函数类型

这更直观:

/** @type {new HTMLElement} **/
const test = null;
// Visual studio hints HTMLElement type, even though HTMLElement does not 
// have a public constructor
const elm = new test();

由你的力量组合:

/** @type {{[x:string]: new ProxyController}} **/
const servers = {
    TCP: ProxyControllerTCP,
    UDP: ProxyControllerUDP,
    UDP_sym: UDPProxyBidirectional
};

我现在可以使用new servers["UDP"]并获取基类的类型提示。