在 Javascript 中调用自定义库函数中的函数

call the function in function of custom library in Javascript

我有一个如下所示的库

(function (bindDropdownAndSetValue) {
    function allFunction() {
        function bindDropDownValue(response, dropdownObject) {
            $.each(response, function (i, e) {
                $('#' + dropdownObject.id).append('<option value=' + e[dropdownObject.value] + '>' + e[dropdownObject.text] + '</option>');
            });
        }

        function dropdownValues(id, value, text) {
            this.id = id;
            this.value = value;
            this.text = text;
        }
    }
    bindDropdownAndSetValue.allFunction = allFunction;
} (bindDropdownAndSetValue));

在上面的示例中,当我调用 bindDropdownAndSetValue.allFunction 时,我想超出 allFunction 中的函数,但它似乎不起作用

当我将库更改为如下内容时

(function (bindDropdownAndSetValue) {
    function bindDropDownValue(response, dropdownObject) {
        $.each(response, function (i, e) {
            $('#' + dropdownObject.id).append('<option value=' + e[dropdownObject.value] + '>' + e[dropdownObject.text] + '</option>');
        });
    }

    function dropdownValues(id, value, text) {
        this.id = id;
        this.value = value;
        this.text = text;
    }
    bindDropdownAndSetValue.bindDropDownValue = bindDropDownValue;
    bindDropdownAndSetValue.dropdownValues = dropdownValues;
} (bindDropdownAndSetValue));

这工作正常,但这里的问题是我需要正确的额外代码行,将 function 一个一个地分配给 bindDropdownAndSetValue.

还有其他更好的方法来纠正这段代码吗?

 (function (bindDropdownAndSetValue) {
    function bindDropDownValue(response, dropdownObject) {
        $.each(response, function (i, e) {
            $('#' + dropdownObject.id).append('<option value=' + e[dropdownObject.value] + '>' + e[dropdownObject.text] + '</option>');
        });
    }

    function dropdownValues(id, value, text) {
        this.id = id;
        this.value = value;
        this.text = text;
    }
    Object.assign(bindDropdownAndSetValue, {bindDropDownValue, dropdownValues });
} (bindDropdownAndSetValue));

保持简短:

bindDropdownAndSetValue.bindDropDownValue = function(response, dropdownObject) {
    $.each(response, function (i, e) {
        $('#' + dropdownObject.id).append('<option value=' + e[dropdownObject.value] + '>' + e[dropdownObject.text] + '</option>');
    });
};
bindDropdownAndSetValue.dropdownValues = function(id, value, text) {
    this.id = id;
    this.value = value;
    this.text = text;
};