JavaScript的模块模式不是功能吗?

tom*_*ato 0 javascript module-pattern

我试图使我的javascript代码遵循模块模式,在这里我要遵循:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

这是我目前的代码,除了运行时调用外,没有语法问题

Pinetime.init()不是函数。

var loosetime = (function () {
    var looseconfig = {
        "format": "DD/MM/YY HH24:MM:SS.s",
        "value": "DD/MM/YY 00:00.00",
        "class": "input",
        "delims": ['/', '-', '_', '.', '|', ',', ' ', ':']
    };

    function loosetime(a, b, c, d, e) {
        var format = a;
        var appendLoc = b;
        var inputVal = c;
        var inputName = d;
        var inputClass = e;
        var inputLength;

        try {
            if (typeof(format) == 'undefined') {
                format = looseconfig.format;
            } else {
                format = parseDateTime(format);
            }

            try {
                if (typeof(inputVal) == 'undefined') {
                    inputVal = looseconfig.value;
                }

                inputLength = inputVal.length - 2;
                var input = document.createElement("input");
                input.setAttribute("name", inputName);
                input.setAttribute("maxlength", inputLength);
                input.setAttribute("size", inputLength);
                input.setAttribute("value", inputVal);
                input.setAttribute("type", "input");
                input.setAttribute("class", inputClass);
                input.setAttribute("onkeypress", "dateTimeRules(event)");
                input.setAttribute("onclick", "resetCursorPos(event)");
                input.setAttribute("loosetime", format);

                try {
                    var element = document.getElementById(appendLoc);

                    element.appendChild(input);
                } catch (e) {
                    window.alert("Error, no Element given to append loosetime to.")
                }
            } catch (e) {
                window.alert("Error, Value is invalid." + e.toString());
            }
        } catch (e) {
            window.alert("Error, Date format missing or invalid.");
        }
    }

    // other code here ...

    return {
        init: loosetime()
    }

    // end private closure then run the closure
    });
Run Code Online (Sandbox Code Playgroud)

理想情况下,我只想让松散时间正常工作,而我不想显式调用构造函数。

例如:宽松时间(“ foo”,“ bar”,“ etc”,“ yolo”,“ 123321”);

我不确定自己在做什么错,我需要返回函数本身而不是别名吗?

Que*_*tin 5

return {
    init: loosetime()
}
Run Code Online (Sandbox Code Playgroud)

init是返回值的调用 loosetime(这是undefined因为该功能不具有return语句)。

()如果要分配函数本身而不是调用它,请删除。


第二个问题是显示模块模式要求您运行闭包:

//end private closure then run the closure
})();
Run Code Online (Sandbox Code Playgroud)

尽管有您的评论,您还是错过了()那里。